Jquery Validator and fields that have been hidden

若如初见. 提交于 2020-01-07 02:22:08

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/6913220/jquery-validator-and-fields-that-have-been-hidden

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!