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

来源:互联网 发布:php 九九乘法表 表格 编辑:程序博客网 时间:2024/06/16 16:58

使用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"       }     });      });});



1 0