在addClassRules中添加组

如何为addClassRules添加的require_from_group添加groups: { }

 $.validator.addClassRules("group_input", { require_from_group: [1,".group_input"] }); 

由于我不想在规则中给出名称,因为名称是动态生成的,我已经使用类进行了validation。 如何添加组,因为我收到每个文本字段的错误消息。

提前致谢。

您不能将groups选项放入.addClassRules()方法。

groups选项只能放在.validate()方法中。 您必须name属性引用字段。

 $('#myform').validate({ // other options, groups: { myGroup: "field1name field2name field3" } }); 

但是,如果您有大量字段,或者在您的情况下, name是动态生成的,您可以构造.validate()外部的名称列表,只需使用变量代替列表。

 var names = ""; // create empty string $('.group_input').each(function() { // grab each input starting w/ the class names += $(this).attr('name') + " "; // append each name + single space to string }); names = $.trim(names); // remove the empty space from the end $('#myform').validate({ // other options, groups: { myGroup: names // reference the string } }); 

工作演示: http : //jsfiddle.net/e99rycac/

资料来源: https : //stackoverflow.com/a/28029469/594235