jQuery验证:$。数据($('形式')[0],'验证')设置返回undefined。 [英] jQuery Validation: $.data($('form')[0], 'validator').settings returns undefined

查看:484
本文介绍了jQuery验证:$。数据($('形式')[0],'验证')设置返回undefined。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.Net MVC项目,我用不显眼的jQuery验证。
添加验证,当一个元素失去焦点,我打电话

I have an ASP.Net MVC Project and I am using unobtrusive jQuery Validation. to add validation when an element looses focus, I am calling

$(document).ready(function () {
    // enable validation when an input loses focus.
    var settngs = $.data($('form')[0], 'validator').settings;
    settngs.onfocusout = function (element) { $(element).valid(); };
});

这是工作的一个项目,而它抛出的另一个项目这个例外,因为$。数据($('形式')[0],'验证')返回undefined($。数据($('形式')[ 0])返回一个空对象):

This is working on one project while it throws this exception on another project because $.data($('form')[0], 'validator') returns undefined ($.data($('form')[0]) returns an empty object):

遗漏的类型错误:无法读取的未定义的属性设置

Uncaught TypeError: Cannot read property 'settings' of undefined

不过jQuery验证时提交按钮做工精细是pressed,所以一切应正确设置。

However jQuery Validation is working fine when the submit button is pressed, so everything else should be set up correctly.

我在body标签的结束加载这些脚本:(上面列出的功能在customvalidations.js所以验证器应用到表格后,就应该执行)

I am loading these scripts at the end of the body tag: (the function listed above is in customvalidations.js so it should execute after the validator is applied to the form)

<script src="/Staffing/Scripts/jquery-2.1.1.js"></script>
<script src="/Staffing/Scripts/globalize/globalize.js"></script>
<script src="/Staffing/Scripts/globalize/cultures/globalize.culture.de-DE.js"></script>
<script src="/Staffing/Scripts/bootstrap.js"></script>
<script src="/Staffing/Scripts/respond.js"></script>
<script src="/Staffing/Scripts/bootstrap-datepicker.js"></script>
<script src="/Staffing/Scripts/bootstrap-datepicker-globalize.js"></script>
<script src="/Staffing/Scripts/locales/bootstrap-datepicker.de.js"></script>
<script src="/Staffing/Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="/Staffing/Scripts/jquery.validate.js"></script>
<script src="/Staffing/Scripts/jquery.validate.globalize.js"></script>
<script src="/Staffing/Scripts/jquery.validate.unobtrusive.js"></script>
<script src="/Staffing/Scripts/localization/messages_de.js"></script>
<script src="/Staffing/Scripts/customvalidations.js"></script>
<script src="/Staffing/Scripts/uiadditions.js"></script>
<script src="/Staffing/Scripts/default.js"></script>

解决方案:
这是code的作品:

Solution: This is the code that works:

$(document).ready(function () {
    // enable validation when an input loses focus.
    var allForms = $('form');
    $.each(allForms, function (key, value) {
        var val = $.data(value, 'validator');
        if (val != undefined) {
            var settngs = val.settings;
            settngs.onfocusout = function (element) { $(element).valid(); };
        }});
});

问题是,如果新的验证插件检查有任何元素present应被验证和我有同样的页面上2形成没有任何验证的输入内容第一种形式。

The problem was that the new Validation plugin Checks if there are any elements present which should be validated and i had 2 forms on the same page with the first form not having any validated input elements.

推荐答案

我刚刚升级到Microsoft.jQuery.Unobtrusive.Validation.3.2.0包。我碰到了同样的问题。难道你也升级到相同的版本?

I just upgraded to Microsoft.jQuery.Unobtrusive.Validation.3.2.0 package. I came across the same problem. Is it possible that you also upgraded to the same version?

如果是这样,我可以提供我的两分钱:)。有点调试后,我发现下面的变化是在jquery.validate.unobtrusive.js提出:

If so, I might offer my two cents :). After a bit of debugging I noticed that the following change was made in jquery.validate.unobtrusive.js:

previous版本:

Previous version:

    var $forms = $(selector)
        .parents("form")
        .andSelf()
        .add($(selector).find("form"))
        .filter("form");

新版本:

        $forms = $selector.parents()
                          .addBack()
                          .filter("form")
                          .add($selector.find("form"))
                          .has("[data-val=true]"); // key point!

关键似乎是:在有(​​[数据-VAL =真])。也就是说,如果你的形式不具有与属性值的所有后代(如输入元素),它不会被验证帮助分析并没有'验证'的数据将被发送到一个表单元素。然后,您如果您尝试在以后的阶段访问形式的验证得到这个未定义错误。

The key seems to be: the has("[data-val=true]"). That is, if your form doesn't have any descendents (like an input element) with that attribute value, it will not be parsed by the validation helper and no 'validator' data will get assigned to that form element. You'll then get this 'undefined' error if you try to access the validator of that form at a later stage.

在我的情况下,表格而这是发生实际上并没有使用任何验证,因此,他们没有这个属性。当时我能够改变我的自定义验证逻辑首先检查验证更改验证设置之前就存在。

In my case, the forms for which this was happening didn't actually use any validation, hence they didn't have this attribute. I was then able to change my custom validator logic to first check if the validator exists before changing validator settings.

希望有所帮助。

这篇关于jQuery验证:$。数据($('形式')[0],'验证')设置返回undefined。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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