【发布时间】:2014-03-20 03:48:07
【问题描述】:
我设法使用 jQuery Validate 插件和this workaround (quoted here) 验证了多个具有相同名称的表单字段,例如first_name[]。
但是,错误消息只显示字段的第一个实例,而不是下一个。
为什么会这样?
为了记录,上面链接中提供的解决方案包括编辑 jquery.validate.js 并将 checkForm 函数内容更改为:
checkForm: function() {
this.prepareForm();
for ( var i = 0, elements = (this.currentElements = this.elements()); elements[i]; i++ ) {
if (this.findByName( elements[i].name ).length != undefined && this.findByName( elements[i].name ).length > 1) {
for (var cnt = 0; cnt < this.findByName( elements[i].name ).length; cnt++) {
this.check( this.findByName( elements[i].name )[cnt] );
}
} else {
this.check( elements[i] );
}
}
return this.valid();
}
那么在调用插件时使用这种参数:
rules: {
"field_name[]": "required"
}
【问题讨论】: