您的位置:首页 > Web前端 > JQuery

【JQ】 validate验证表单时多个name相同的元素的解决办法

2017-05-02 23:42 585 查看
使用jQuery.validate插件http://jqueryvalidation.org/,当节点的name相同时候,脚本特意忽略剩余节点,导致所有相关节点的errMsg都显示在第一个相关节点上。

$(function () {
if ($.validator) {
$.validator.prototype.elements = function () {
var validator = this,
rulesCache = {};
return $([]).add(this.currentForm.elements)
.filter(":input")
.not(":submit, :reset, :image, [disabled]")
.not(this.settings.ignore)
.filter(function () {
var elementIdentification = this.id || this.name;
!elementIdentification && validator.settings.debug && window.console && console.error("%o has no id nor name assigned", this);
if (elementIdentification in rulesCache || !validator.objectLength($(this).rules()))
return false;
rulesCache[elementIdentification] = true;
return true;
});
};

}

});在页面上引入以上代码,然后给相关节点加上id属性,当name属性相同时候会以id属性来验证
以下代码添加验证规则

$(function(){
$("#myform").validate();
$("[name=email]").each(function(){
$(this).rules("add", {
required: true,
email: true,
messages: {
required: "请输入正确email"
}
});
});
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  jquery validate
相关文章推荐