jQuery验证插件在IE中失败(jQuery Validate plugin fails in I

2019-07-20 07:20发布

我有我使用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>

Answer 1:

您的代码:

$.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/

正如你所看到的,它的工作在Internet Explorer 9在Windows 7 ...



Answer 2:

IE不会有额外的逗号发挥很好。

请注意,邮件属性后,额外的逗号。 删除它取胜。

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


文章来源: jQuery Validate plugin fails in IE