jQuery Validate插件在IE中失败 [英] jQuery Validate plugin fails in IE

查看:138
本文介绍了jQuery Validate插件在IE中失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在使用jQuery验证的表单.该表单在除IE(无论哪个版本的IE)之外的所有浏览器中均能完美运行.我还阅读了其他一些有关使其正常工作的信息,但这些信息并没有帮助.他们中的一些人提到摆脱尾随逗号.我试过了,但是没有用.任何帮助将不胜感激.

I have a form that I'm using jQuery validate with. The form works perfectly in every browser except for IE (regardless of which version of IE). I've read several other posts about getting it to work and they didn't help. A couple of them mentioned getting rid of trailing commas; I tried that and it didn't work. Any help would be greatly appreciated.

这是我的验证码:

<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之外的所有浏览器.

I even tried simplifying the validation code as much as possible. After simplifying it it still worked in all browsers except 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中,并且可以按照下面的演示进行操作.

You should wrap your code in a DOM ready and it's working as per the demo below.

$(document).ready(function() {

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

});

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

Working Demo: http://jsfiddle.net/HNK6w/

如您所见,它正在Windows 7的Internet Explorer 9中运行...

As you can see, it's working in Internet Explorer 9 on Windows 7...

这篇关于jQuery Validate插件在IE中失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆