简体   繁体   English

如何使 jQuery 有效 function 在 IE 上可靠工作?

[英]How to make the jQuery valid function work reliably on IE?

I have a problem on jQuery valid function.我在 jQuery 有效 function 上有问题。 When on IE, it doesn't work, the valid always return true.在 IE 上,它不起作用,valid 总是返回 true。 I used this code: client side validation with dynamically added field我使用了这段代码: 带有动态添加字段的客户端验证

Here's the chart:这是图表:

                    Chrome      IE

jquery-1.6.1        works       not working
jquery-1.4.4        works       works

1.6 doesn't work on IE too. 1.6 也不适用于 IE。 However, 1.4.4 jQuery valid works on IE.但是,1.4.4 jQuery 有效适用于 IE。

Here's the jsFiddle-friendly test (test this as local html):这是对 jsFiddle 友好的 测试(将其作为本地 html 测试):

<!--
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script> -->

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script>

<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.8/jquery.validate.min.js" type="text/javascript"></script>

<script src="http://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>



<form id="XXX">
    <input type="submit" id="Save" value="Save">

</form>


<script type="text/javascript">

    // sourced from https://stackoverflow.com/questions/5965470/client-side-validation-with-dynamically-added-field
    // which I do think don't have a bug
    (function ($) {


        $.validator.unobtrusive.parseDynamicContent = function (selector) {
            //use the normal unobstrusive.parse method
            $.validator.unobtrusive.parse(selector);

            //get the relevant form
            var form = $(selector).first().closest('form');

            //get the collections of unobstrusive validators, and jquery validators
            //and compare the two
            var unobtrusiveValidation = form.data('unobtrusiveValidation');
            var validator = form.validate();

            $.each(unobtrusiveValidation.options.rules, function (elname, elrules) {
                if (validator.settings.rules[elname] == undefined) {
                    var args = {};
                    $.extend(args, elrules);
                    args.messages = unobtrusiveValidation.options.messages[elname];
                    $('[name=' + elname + ']').rules("add", args);
                } else {
                    $.each(elrules, function (rulename, data) {
                        if (validator.settings.rules[elname][rulename] == undefined) {
                            var args = {};
                            args[rulename] = data;
                            args.messages = unobtrusiveValidation.options.messages[elname][rulename];
                            $('[name=' + elname + ']').rules("add", args);
                        }
                    });
                }
            });
        }
    })($);
    // ...sourced from others


    // my code starts here...
    $(function () {

        var html = "<input data-val='true' " +
           "data-val-required='This field is required' " + "name='inputFieldName' id='inputFieldId' type='text'/>";
        $("form").append(html);

        var scope = $('#XXX');

        $.validator.unobtrusive.parseDynamicContent(scope);




        $('#Save').click(function (e) {
            e.preventDefault();
            alert(scope.valid());
        });

    });
    // ...my code ends here

</script>

UPDATE更新

I tried my code on jsFiddle, it has side-effect, the jQuery 1.6's valid is working on IE.我在 jsFiddle 上尝试了我的代码,它有副作用,jQuery 1.6 的有效版本正在 IE 上运行。 Don't test this code on jsFiddle.不要在 jsFiddle 上测试此代码。 Test this code on your local html在本地 html 上测试此代码

This problem has been solved.这个问题已经解决了。 try version 1.8.1.尝试版本 1.8.1。

Download jQuery validation plugin下载 jQuery 验证插件

Hi I also got the same problem and I have updated my both scripts file to the latest one and everything is working very fine on my side.嗨,我也遇到了同样的问题,我已经将我的两个脚本文件都更新到了最新的,一切都在我这边工作得很好。 Go to jquery.com and check for the latest jquery code file. Go 到 jquery.com 并检查最新的 ZD223E143918876078349EZC2 代码文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM