jquery 使用动态输入名称验证唯一性

jquery validation for unique with dynamic name of input

我使用了以下 jquery 代码

jQuery.validator.addMethod("unique", function(value, element, params) {
    var prefix = params;
    var selector = jQuery.validator.format("[name!='{0}'][name^='{1}'][unique='{1}']", element.name, prefix);
    var matches = new Array();
    $(selector).each(function(index, item) {
        if (value == $(item).val()) {
            matches.push(item);
        }
    });

    return matches.length == 0;
}, "Value is not unique.");

jQuery.validator.classRuleSettings.unique = {
    unique: true
};
$("#myform").validate();

$("#validate").click(function() {
    $("#myform").valid();
});

但由于相同的输入名称无法正常工作,请在此处查看 https://jsfiddle.net/bgzBY/147/

但如果我使用不同的名称,请检查 http://jsfiddle.net/mysteryh/bgzBY/

那么如何解决呢?

这与你的名字无关。您的脚本未正确加载。阻止加载混合活动内容“http://ajax.microsoft.com/ajax/jquery.validate/1.8/jquery.validate.min.js”这是控制台中显示的错误。

不能在多个输入字段中使用相同的name。插件的此要求没有解决方法。 (这是插件跟踪输入元素的方式。)

So how to fix it?

必须对每个输入使用唯一的namecurrency[1]currency[2]