开发者

Jquery Validator and fields that have been hidden

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>

And then call:

$('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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜