且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将jQuery validate插件与强类型枚举一起使用?

更新时间:2023-09-29 14:14:34

[和]字符对于jQuery validate插件不是严格无效的.这只是草率的编码,没有考虑在名称/标识中包含这些字符的可能性.

The [ and ] characters aren't strictly invalid for jQuery validate plugin. It's just a sloppy coding which doesn't account for possibility to have those chars in name/id.

甚至还有GitHub问题处理ASP.NET MVC输入数组异常为此.

There is even a GitHub issue Handling ASP.NET MVC Input Arrays Exception for this.

我所做的是我从问题中取出了代码(它本身就有一个小问题),并将此hack添加到了我的JavaScript初始化代码中:

What I did is I took the code from issue (it has a small problem on it's own) and added this hack on my JavaScript initialization code:

$.validator.prototype.idOrName = function(element) {
    return this.groups[element.name]
        || (this.checkable(element)
            ? element.name
            : element.id || element.name)
        .replace("[", "\\[")
        .replace("]", "\\]");
}

那可能不是最美或***的解决方案,但对我有用.至少直到它在jQuery中修复为止.

That might not be pretiest or best solution but it works for me. At least till it's fixed in jQuery.

无需使用此功能,因为它可以与1.13.1一起使用(有关讨论,请参见上面的问题).请注意,尽管我尚未检查自己,但ASP.NET MVC仍可能与1.13.0捆绑在一起