我如何禁用只读字段jQuery验证? [英] How can I disable jquery validation on readonly fields?

查看:226
本文介绍了我如何禁用只读字段jQuery验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的该产品采用与MVC3在幕后工作jQuery验证。我有切换为只读的字段(基于一个下拉的与否值> 0)或没有。

诀窍是,这个领域需要的时候它不是只读的需要,需要不出错提交时,它是只读的,无论是否有数据。

我应该如何去这样做呢?


解决方案

  $。validator.setDefaults({
    忽略:':隐藏,[只读=只读]'
});

忽略参数的默认值为:隐藏。通过使用上述code重置默认情况下,确保JQuery验证仍然无法在隐藏的表单元素的行为,也没有对具有只读=只读元素的行为属性。

回复评论

这听起来很奇怪,你可能要发布一些code,以获得更多的帮助。有一件事你可以尝试删除验证错误是手动验证表单,当你切换只读属性,这样的事情也许是:

  $('#the_text_box')ATTR('只读','只读')。
$ .validator.setDefaults(:隐藏');
$('形式')有效();
$ .validator.setDefaults(:隐藏,[只读=只读]');

您也可以做同样的事情在初始页面加载期间避免即时验证错误消息。

  $ validator.setDefaults(:隐藏,[只读=只读]');
$('形式')有效();

The product I am working for uses JQuery validation with MVC3 working behind the scenes. I have a field that I toggle as read only (based on whether or not a dropdown's value is > 0) or not.

The trick is, this field needs to be required when it is not read-only, and needs to submit without errors when it is read-only, whether or not it has data.

How should I go about doing this?

解决方案

$.validator.setDefaults({
    ignore: ':hidden, [readonly=readonly]'
});

The default for the ignore parameter is ":hidden". By resetting the default using the above code, you make sure that jquery validate still does not act on hidden form elements, but also does not act on elements that have a readonly="readonly" attribute.

Reply to comments

That sounds odd, you may have to post some code to get more help. One thing you could try to remove the validation errors is to manually validate the form when you toggle the readonly attribute, something like this perhaps:

$('#the_text_box').attr('readonly', 'readonly');
$.validator.setDefaults(':hidden');
$('form').valid();
$.validator.setDefaults(':hidden, [readonly=readonly]');

You could also do something similar during the initial page load to avoid the instant validation error messages.

$.validator.setDefaults(':hidden, [readonly=readonly]');
$('form').valid();

这篇关于我如何禁用只读字段jQuery验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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