即使没有错误,为什么表单是错误消息?

时间:2014-02-28 14:48:53

标签: javascript jquery html

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

alert("Fill in the textarea");

即使在填写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/

1 个答案:

答案 0 :(得分:5)

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