jQuery验证程序和已隐藏的字段 [英] Jquery Validator and fields that have been hidden

查看:64
本文介绍了jQuery验证程序和已隐藏的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取 Jquery validaten插件,以便在提交时不验证隐藏字段.例如,如果我们有这样的HTML:

I'm trying to get the Jquery validationn plugin to not validate hidden fields on submission. For example, if we have HTML like this:

<div id="1">
  <input type="text" class="digits">
</div>
<div id="2">
  <input type="text" class="digits">
</div>

然后致电:

$('div#2').hide();

并提交表单,即使第二个输入可能输入了一些错误的数据,该表单仍应提交.我想更改验证代码,但找不到相关的片段.

and submit the form, even though the second input may have some wrong data inputted the form should still submit. I wanted to change the code of validate, but couldn't find the relevant fragments.

推荐答案

从字段中删除验证的最简单方法是向字段添加disabled属性.

The simplest way to remove validation from fields is to add disabled attribute to them.

var $div2 = $('div#2');
$div2.hide();

$('input, select, textarea', $div2).attr('disabled', 'disabled');

并且它们不会被验证.但这也会导致禁用的字段无法在服务器上提交.如果您不需要在服务器上提交这些手动隐藏的字段,则技术不错.

And they will not be validated. But this also causes disabled fields not to be submitted on server. If you do not need these manually hidden fields to be submitted on server, technique is good.

另一种方法是使用删除规则添加规则函数.这当然会更加复杂,因为您将必须为每个输入一个一个地添加和删除每个规则.

And another way is to manually add and remove all of the validation rules from the element, using remove rules and add rules functions. This is of course more complicated, as you will have to add and remove each rule one by one for each input.

这篇关于jQuery验证程序和已隐藏的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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