Javascript 为什么表单是错误的,即使在没有错误的情况下也会出现错误消息?

Javascript 为什么表单是错误的,即使在没有错误的情况下也会出现错误消息?,javascript,jquery,html,Javascript,Jquery,Html,在以下代码中,错误如下所示: 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%" cellpad

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

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 Fiddle链接如下所示: 使用
textfield.val()
而不是
textfield.text()
方法。

您应该为
使用
val()
,而不是
text()
。另外,在调用另一个jQuery函数
val()
之前,不需要在其中重新包装
textfield
。至于原因,请参阅以下帖子:
$(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");
        }
      });
    });
  });