我已使用此规则验证用户的帐单邮寄地址(如果他想要帐单)(输入#billyes已选中),这是我当前的代码:
//BILLING VALIDATION
legal_id: {
required: {
depends: function(element) {
return $('#billyes').is(":checked");
}
}
},
billing_name: {
required: {
depends: function(element) {
return $('#billyes').is(":checked");
}
}
},
billing_adress: {
required: {
depends: function(element) {
return $('#billyes').is(":checked");
}
}
},
.... ETC
线也重复 return $('#billyes').is(":checked");
我想做这样的事情:
//BILLING VALIDATION
legal_id,billing_name,billing_adress,etc: {
required: {
depends: function(element) {
return $('#billyes').is(":checked");
}
}
}
这可能吗?还有其他选择吗?
谢谢。
legal_id,billing_name,billing_adress,etc: {
在该.validate()
方法中,您根本无法做任何事情。rules
选项内的对象文字必须key:value
成对,其中key
只能是单个字段name
。
但是,有一种方法可以一次将同一规则应用于多个字段:
您可以将.rules()
方法与jQuery结合使用.each()
。但是您将需要一个聪明的jQuery选择器来一次将它们全部定位。就像“开始于”选择器或class
。您也不能使用“开头为billing_
”,因为您也有legal_id
,因此您需要将a分配class
给这些字段或执行其他操作。
$('.yourFields').each(function() {
$(this).rules('add', {
required: {
depends: function(element) {
return $('#billyes').is(":checked");
}
}
});
});
否则,在.validate()
方法内,没有快捷方式。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句