【问题标题】:Why the form is error message is coming even after not having the error?为什么即使没有错误,表单也会出现错误消息?
【发布时间】:2014-04-01 15:25:11
【问题描述】:

在以下代码中,错误如下:

alert("Fill in the textarea");

即使在填充文本区域后也会出现错误消息。如何避免显示错误消息?

以下是我的 HTML 代码:

<form name="question_issue_form" id="question_issue_form" action="question_issue.php">
          <table class="trnsction_details" width="100%" cellpadding="5">
            <tbody>    
              <tr>
                <td></td>
                <td>
                  <input type="checkbox" name = "que_issue[]" value = "Question is wrong" id ="chkQueWrong">Question is wrong</input>
                </td>
              </tr>
              <tr>
                <td></td>
                <td><input type="checkbox" name = "que_issue[]" value = "Answers are wrong" id ="chkAnsWrong">Answers are wrong</input></td> 
              </tr>
              <tr>
                <td></td>
                <td><input type="checkbox" name = "que_issue[]" value = "Question direction is incorrect" id ="chkDirIncorrect">Question direction is incorrecct</input></td>                
              </tr>
              <tr>
                <td></td>
                <td><input type="checkbox" name = "que_issue[]" value = "Other" id ="chkOther">Other</input></td>          
              </tr>
              <tr>
                <td></td>
                <td class="set_message" style="display:none;"><textarea name="que_issue_comment" rows="4" cols="25" maxlength="100"></textarea></td>      
              </tr>
              <tr>
                <td></td>
                <td><input type="submit" name="submit" value="Submit" id="report_question_issue"/></td>
              </tr>
            </tbody>
          </table>
        </form>

以下是JS代码:

$(document).ready(function() {
  $('#chkOther').click(function() {
    $('.set_message').toggle(this.checked);
  });

  //This function is use for edit transaction status
  $(document).on('click', '#report_question_issue', function(e) {

    e.preventDefault();

    //$.colorbox.close();
    //for confirmation that status change
    var ans = confirm("Are you sure to report the question issue over?");
    if (!ans) { //alert("Yes its false");
      return false;
    }

    var post_url = $('#post_url').val();

    var checkbox = document.getElementsByName("que_issue[]");

    var checkedAtLeastOne = false;

    $('input[type="checkbox"]').each(function() {
      if ($(this).is(":checked")) {
        checkedAtLeastOne = true;
        return false; //to break from each()
      }
    });

    if (checkedAtLeastOne) {
      //alert("In If");
      var other = $("#chkOther");
      var textfield = $("[name=que_issue_comment]");
      if (other.is(":checked")) {
        if ($.trim($(textfield).text()).length == 0) {
        //error
          alert("Fill in the textarea");
          return false;
        } else {
          return true;
        }
      }
    } else {
        alert("Check at least one checkbox");
        return false;
      }

      $.ajax({
        type: "POST",
        url: post_url,
        data: $('#question_issue_form').serialize(),
        dataType: 'json',
        success: function(data) {
          alert("The values have been inserted");
        }
      });
    });
  });

JS Fiddle链接如下: http://jsfiddle.net/VqmzL/11/

【问题讨论】:

标签: javascript jquery html


【解决方案1】:

使用textfield.val() 代替textfield.text() 方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-31
    • 2015-03-08
    • 2021-09-20
    • 1970-01-01
    • 2017-07-17
    • 2016-12-15
    • 1970-01-01
    相关资源
    最近更新 更多