jquery验证仅适用于可见控件

时间:2022-11-28 16:37:50

I am using jquery validation for required field vaidation.

我正在使用jquery验证来进行必要的字段验证。

$("#registerform").validate();

This fires for the controls which are hidden too.

这会触发隐藏的控件。

For ex.: When the user selects Others option in How you find us? in register form, its required to fill the textbox for others option.

例如:当用户在如何找到我们时选择其他选项?在注册表格中,需要填写其他人的文本框选项。

But this fires even when the control is hidden.

但即使控件被隐藏,这也会触发。

How to do it in jquery validation?

如何在jquery验证中执行此操作?

3 个解决方案

#1


34  

I haven't used the jQuery validation before but according to the doco there's an ignore field that you can pass a jQuery selector to. By using the :visible selector you should be able to do this.

我以前没有使用过jQuery验证,但是根据doco有一个可以传递jQuery选择器的ignore字段。通过使用:visible选择器,您应该能够执行此操作。

I believe this is what you're after

我相信这就是你所追求的

$("#registerform").validate({ignore:":not(:visible)"});

#2


3  

You may try to add :visible to the selector itself:

您可以尝试添加:对选择器本身可见:

$("#registerform :visible")

#3


0  

Please also try this way

也请尝试这种方式

 if ($(#registerform).is(":visible")) {
      $(#registerform).validate();
  }

#1


34  

I haven't used the jQuery validation before but according to the doco there's an ignore field that you can pass a jQuery selector to. By using the :visible selector you should be able to do this.

我以前没有使用过jQuery验证,但是根据doco有一个可以传递jQuery选择器的ignore字段。通过使用:visible选择器,您应该能够执行此操作。

I believe this is what you're after

我相信这就是你所追求的

$("#registerform").validate({ignore:":not(:visible)"});

#2


3  

You may try to add :visible to the selector itself:

您可以尝试添加:对选择器本身可见:

$("#registerform :visible")

#3


0  

Please also try this way

也请尝试这种方式

 if ($(#registerform).is(":visible")) {
      $(#registerform).validate();
  }