场景
在一个form表单中有多个相同的name需要校验
<form id="form" method="post" action="http:xxxx.com">
<input type="text" name="username" />
<input type="text" name="username" />
<input type="submit" value="提交" />
</form>
问题
在一个表单中如果有多个相同的name出现时,只会校验第一个name
解决方法
if ($.validator) {
$.validator.prototype.elements = function () {
var validator = this,
rulesCache = {};
return $(this.currentForm)
.find("input, select, textarea")
.not(":submit, :reset, :image, [disabled]")
.not(this.settings.ignore)
.filter(function () {
if (!this.name && validator.settings.debug && window.console) {
console.error("%o has no name assigned", this);
}
rulesCache[this.name] = true;
return true;
});
}
}
然后在input上添加不同的id就可以了,当多个name相同的时候,会根据input的id去验证