忽略所有隐藏的 div 但不是 jQuery 验证中的一个

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20287567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 01:06:22  来源:igfitidea点击:

Ignore all hidden div but not one in jQuery validation

jqueryjquery-validatehidden

提问by novellino

I am using jQuery validation in my form http://jqueryvalidation.org/documentation/

我在我的表单中使用 jQuery 验证 http://jqueryvalidation.org/documentation/

I want to add the validation to all my fields but I want to ignore the hidden div's that have the class my_item.

我想将验证添加到我的所有字段,但我想忽略div具有该类的隐藏的my_item

So here is my jQuery:

所以这是我的 jQuery:

 $("#myform").validate({ 
    ignore: ":hidden"
 });

How is it possible to exclude from this ignore case, the divs that have the class my_item. So something like $(":hidden").not(.my_item).

如何从这种忽略情况中排除具有 class 的 div my_item。所以像$(":hidden").not(.my_item).

回答by techfoobar

You can use the :not()selector:

您可以使用:not()选择器:

ignore: ":hidden:not(.my_item)"

回答by Dimitri van der Vliet

Accepted answer is perfectly fine but when you need some more control than the jQuery selector provides. You could pass a function that tests each element.

接受的答案非常好,但是当您需要比 jQuery 选择器提供的更多控制时。 您可以传递一个测试每个元素的函数。

for example:

例如:

ignore: function (index, el) {
   var $el = $(el);

   if ($el.hasClass('always-validate')) {
       return false;
   }

   // Default behavior
   return $el.is(':hidden');
},