jQuery validate plugin – showErrors选项 – errorContainer不会隐藏/显示
我一直在努力为我的网站提供一个很好的validation解决方案,但我遇到了不同选项的问题。 我已经彻底阅读了文档并查看了示例,但我仍然遇到问题。
我的表格在表格中。 我希望每一行都有它自己的错误行,它通常是隐藏的,但是会在适当的时候为每一行显示。
以下选项隐藏并显示错误行正常,但每个错误行中显示的错误消息是每个错误消息的整个串联:
$('#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选项,如下所示:
$('#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
。 我在这做错了什么?
非常感谢
如果查看showErrors方法的validation插件的源代码,您可以:
this.settings.showErrors ? this.settings.showErrors.call( this, this.errorMap, this.errorList ) : this.defaultShowErrors();
这意味着如果为此方法提供自定义处理程序,则不会执行默认行为。
您可以在自己的showErrors处理程序结束时调用this.defaultShowErrors(),该处理程序将变为:
showErrors: function(errorMap, errorList) { $.each(errorMap, function(key, value) { $('#'+key).parent().next().children('.appValueColumn') .html(errorMap[key]); }); this.defaultShowErrors(); });
希望这可以帮助 !
确保使用这些行上没有前导点的类名:
errorContainer: 'errorRow', errorLabelContainer: 'errorRow appValueColumn',