jQuery validate插件-showErrors选项-errorContainer未取消隐藏/显示 [英] jQuery validate plugin - showErrors option - errorContainer not un-hiding / showing
问题描述
我一直在尝试获得一个不错的验证解决方案以适合我的网站,但是我在使用不同的选项时遇到了麻烦.我已经仔细阅读了文档并查看了示例,但是仍然遇到问题.
I've been trying to get a nice validation solution to work for my site, but I'm having trouble with the different options. I have read the docs thoroughly and looked at examples, but I'm still having issues.
我的表格在表格中.我希望每行下面都有自己的错误行,该行通常是隐藏的,但会在适当的时候为每行显示.
My form is in a table. I want each row to have it's own error row beneath it, which would ordinarily be hidden, but which would show for each row as appropriate.
以下选项隐藏并显示了错误行,但每个错误行中显示的错误消息是每条错误消息的整体串联:
The following options hid and showed the error rows fine, but the error message shown in each error row was the whole concatenation of every error message:
$('#myform').validate({
rules: {
firstName: "required",
lastName: "required"
},
messages: {
firstName: "Enter your first name.",
lastName: "Enter your last name."
},
errorContainer: '.errorRow',
errorLabelContainer: '.errorRow.appValueColumn',
errorPlacement: function(error, element) {
error.appendTo( element.parent().next() );
}
});
所以我尝试如下使用showErrors选项:
so I tried to use the showErrors option as follows:
$('#myform').validate({
rules: {
firstName: "required",
lastName: "required"
},
messages: {
firstName: "Enter your first name.",
lastName: "Enter your last name."
},
errorContainer: '.errorRow',
errorContainer: '.errorRow.appValueColumn',
showErrors: function(errorMap, errorList) {
$.each(errorMap, function(key, value) {
$('#'+key).parent().next().children('.appValueColumn').html(errorMap[key]);
});
好吧,现在所有错误都被分离并显示在正确的位置,但是我无法显示.errorRows
.我在这里做什么错了?
Well, now the errors are all separated and shown in the correct place, but I can't get the .errorRows
to show. What am I doing wrong here?
非常感谢
推荐答案
如果查看showErrors方法的验证插件的源代码,您将拥有:
If you look at the source of the validation plugin for the showErrors method, you have:
this.settings.showErrors
? this.settings.showErrors.call( this, this.errorMap, this.errorList )
: this.defaultShowErrors();
这意味着,如果您为此方法提供了自定义处理程序,则不会执行默认行为.
This means that if you provide a custom handler for this method, the default behavior is not executed.
您可以在自己的showErrors处理程序的末尾调用this.defaultShowErrors(),该处理程序将变为:
You could either call this.defaultShowErrors() at the end of your own showErrors handler, which will become:
showErrors: function(errorMap, errorList) {
$.each(errorMap, function(key, value) {
$('#'+key).parent().next().children('.appValueColumn')
.html(errorMap[key]);
});
this.defaultShowErrors();
});
希望这会有所帮助!
这篇关于jQuery validate插件-showErrors选项-errorContainer未取消隐藏/显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!