使用jQuery非侵入式验证禁用对元素的验证 [英] Disable validation for an element with jQuery Unobtrusive Validation

查看:71
本文介绍了使用jQuery非侵入式验证禁用对元素的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在生成页面上的元素列表,并且所有元素都附加有验证器。当我查看HTML源代码时,会看到类似的内容:

I am generating a list of elements on a page, and they all have validators attached to them. When I look in the HTML source, I see something like:

<input type="text" id="email" name="email" data-val-required="No valid email address!" data-val="true">

我需要一种方法来动态启用/禁用此类元素的验证。我试图通过将 data-val 属性设置为 false 来启用/禁用该属性,然后将其恢复为 true 。但这似乎并没有回应;

I need a way to dynamically enable/disable the validation for such an element. I tried to enable/disable the data-val attribute by setting it to false and then back to true. But it doesn't seem to response to that; the validation is always there!

有人知道如何动态地启用/禁用某些字段上的验证器吗?

Does anyone have any idea how I can enable/disable validators on certain fields dynamically?

推荐答案

我实际上找到了一个更适合我需要的解决方案。我可以执行以下操作:

I actually found a solution that fits my needs better. I can do the following:

$(function() {
    var settngs = $.data($('form')[0], 'validator').settings;
    settngs.ignore = ".ignore";
});

通过添加或删除类名,我可以切换我想要的任何元素c $ c>忽略来自某个元素。

And with that i can 'toggle' any element that i want by adding or removing the classname ignore from an element.

这篇关于使用jQuery非侵入式验证禁用对元素的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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