jQuery验证-如何在errorPlacement回调中分隔错误? [英] jQuery Validation - how to separate errors within the errorPlacement callback?
问题描述
errorPlacement: function (error, element) {
$('#div').append(error.html());
}
我想在调用此函数时在我的网页中的div内附加错误列表.现在,我只是调用error.html(),它为我提供了串联在一起的message函数中列出的所有错误的完整列表.
I want to append a list of errors within a div in my webpage upon calling this function. Right now I am just calling error.html() which is giving me the whole list of errors listed within the messages function concatenated together.
例如div:
需要标题,字数必须在50到65之间 必填
Title is requiredNumber of words must be between 50 and 65Picture is Required
如果需要,我可以提供更多代码.
I can provide more code if needed.
推荐答案
errorPlacement
是用于将每个错误放在每个单独的输入字段旁边的选项/功能.如果要列出错误列表,则不能使用此选项.
errorPlacement
is the option/function used to place each individual error next to each individual input field. This is not the option to use if you want a list of errors.
如果要创建错误消息列表,则可以使用 showErrors
选项.有关用法示例,请参见文档.
If you want to create a list of error messages, then you would use the showErrors
option. Refer to the documentation for example usage...
showErrors: function(errorMap, errorList) {
$("#summary").html("Your form contains "
+ this.numberOfInvalids()
+ " errors, see details below.");
this.defaultShowErrors();
}
通用演示: http://jsfiddle.net/0k0vL1b0/
Generic DEMO: http://jsfiddle.net/0k0vL1b0/
如果要隐藏每个字段旁边的消息,只需注释掉this.defaultShowErrors()
行并编写一些代码以从提供的errorList
参数中提取消息.
If you want to suppress the messages next to each field, simply comment out the this.defaultShowErrors()
line and write some code to extract the messages from the provided errorList
argument.
演示2: http://jsfiddle.net/0k0vL1b0/1/
DEMO 2: http://jsfiddle.net/0k0vL1b0/1/
这篇关于jQuery验证-如何在errorPlacement回调中分隔错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!