jQuery validate插件-仅在可见时验证隐藏的元素 [英] jQuery validate plug in - validating hidden elements only when visible
问题描述
我有一个表格,其中包含您的帐单邮寄地址是否与您的收货地址相同"字段.如果用户单击单选按钮否",则显示隐藏的ul#billingAddress.如果ul#billingAddress包含ul#billingAddress,则该字段是必填字段,即ul具有display:block.
I have a form that has a "Is you billing address the same as your shipping address" field. If the user clicks the radio button "No" the hidden ul#billingAddress is shown. The fields contained in ul#billingAddress are required if it is visible, that is if the ul has display:block.
我如何为jquery validate.js编写一个自定义的addMethod,仅当该字段可见时才需要这些方法?这就是我所无法使用的.
How do I write a custom addMethod for jquery validate.js that requires those only if the field is visible? This is what I have that isn't working.
$.validator.addMethod ("BillingSameAsShipping", function(value, element) {
var billingFields = ['billingAddress1','billingAddress2','billingCity','billingState','bilingZip']
if ($("#billingAddress").is('visible') && billingFields.val('') {
return false;
} else
return true;
}, "Please fill in the required billing information" );
这显然是被顶住的.我需要使它适用于var中的每个变量.
This, obviously is jacked. I need to make it work for each one that is in the var.
谢谢!
推荐答案
可见检查的问题是这一部分:.is('visible')
它必须是:.is(':visible')
以使用
The problem with the visible check is this part: .is('visible')
it needs to be: .is(':visible')
to use the :visible
selector.
或者,您可以使用 ignore
选项您想轻松一点,就像这样:
As an alternative, you can use the ignore
option to do what you want a bit easier, like this:
$("#myForm").validate({
//other options
ignore: ':hidden'
});
如果需要,此方法可让您使用标准的必需规则.
This approach lets you use the standard required rules if you want.
这篇关于jQuery validate插件-仅在可见时验证隐藏的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!