IE中的jQuery Validate插件失败

时间:2013-02-21 15:37:23

标签: jquery internet-explorer jquery-validate

我有一个表单,我正在使用jQuery验证。除IE之外,该表单在每个浏览器中都能完美运行(无论哪个版本的IE)。我已经阅读了其他一些关于让它上班的帖子,但他们没有帮助。他们中的一些人提到摆脱尾随的逗号;我试过了,但没用。任何帮助将不胜感激。

这是我的验证码:

<script>
$(document).ajaxStop(function(){
  setTimeout("window.location = 'index.php'",60000);
});
        $.validator.addMethod("zero", function(input, element) {
        return ! input || input.match(/^[0]/);
}, "This field must start with a zero");
        $("#studentid").validate({
            debug: false,
            rules: {
                id: {required: true, zero: true}
            },
            messages: {
                id: {required: "Please enter the student's ID number.", zero: "Student ID must start with a zero."}
            },                 
            submitHandler: function(form) {

      $.ajax({
          url: 'tutoring.php',
          type: 'POST',
          data: $("#studentid").serialize(),         
          success: function(data) {
        $("#id").val("");
        $("#results").empty();
        $("#results").append(data);
        }
      });

      return false;
   }
});

</script>

这是我的HTmL:

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>
<style type="text/css">
label.error {
  width: 350px;
  display: inline;
  color: red;
}
</style>
</head>

<div align="center">
  <h1>Welcome to the Ojeda Middle School Tutoring Website</h1>
</div>
<p>You can use this page to view tutoring information for the current week.</p>
<p>Please enter a student ID number below then click Submit.</p>
<form id='studentid' class='studentid' action='' method='get'>
  <p>
    <label for="id">Student ID:</label>
    <input type="text" name="id" id="id" /><br>
    <button id='submit' class='submit'>Submit</button>
  </p>
</form>
<p>
<div id="results"></div>

我甚至尝试尽可能简化验证码。在简化之后,它仍然适用于除IE之外的所有浏览器。

简化验证码:

<script>
$(document).ajaxStop(function(){
  setTimeout("window.location = 'index.php'",60000);
});

        $("#studentid").validate({
            debug: false,
            rules: {
                id: {required: true}
            },
            messages: {
                id: {required: "Please enter the student's ID number."}
            },                 

});

</script>

2 个答案:

答案 0 :(得分:4)

IE无法使用额外的逗号。

请注意messages属性后面的额外逗号。删除它赢得。

$("#studentid").validate({
        debug: false,
        rules: {
            id: {required: true}
        },
        messages: {
            id: {required: "Please enter the student's ID number."}
        },  <--------------------

答案 1 :(得分:4)

您的代码:

$.validator.addMethod("zero", function(input, element) { ... });
$("#studentid").validate({
    // options
});

您应该将代码包装在DOM中,并按照以下演示进行操作。

$(document).ready(function() {

    $.validator.addMethod("zero", function(input, element) { ... });
    $("#studentid").validate({ // <-- initialize the plugin once
        // options
    });

});

工作演示:http://jsfiddle.net/HNK6w/

如您所见,它在Windows 7上的Internet Explorer 9中工作......

enter image description here