jQuery 验证插件在 IE 中失败

标签 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>

最佳答案

您的代码:

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

关于jQuery 验证插件在 IE 中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15006185/

相关文章:

jquery - 链接在 ie8 兼容模式下不工作

javascript - 除非在 URL 中提供参数,否则 IE8 AJAX GET setRequestHeaders 不起作用

internet-explorer - 为什么 IE 会在兼容模式之外重复发送图像的 GET 请求?

javascript - 如何使用 jquery 验证重置错误消息?

javascript - 使用 jquery 验证插件验证表单

jquery - 我如何选择 .main-nav 中没有将 href 设置为 # 的所有 anchor 链接

jquery - Ajax/Jquery 自动完成 JSON 数据

jQuery 验证仍然提交无效字段

javascript - jQuery 淡入和淡出消息列表

html - IE 8 上的背景图片