jQuery验证 - 如何在errorPlacement回调中分离错误?

时间:2015-06-03 19:02:07

标签: jquery jquery-validate

 errorPlacement: function (error, element) {
 $('#div').append(error.html()); 
 }

我想在调用此函数时在我的网页中的div中附加错误列表。现在我只是调用error.html(),它给出了连接在一起的消息函数中列出的错误的完整列表。

ex中的div:

  

标题是必需的字数必须在50到65之间。图片是   需要

如果需要,我可以提供更多代码。

1 个答案:

答案 0 :(得分:1)

<h4>Empty p</h4> <p></p> <p>p tag with text.</p> <h4>Empty span</h4> <span></span> <span>span tag with text.</span>是用于将每个错误放在每个单独输入字段旁边的选项/函数。如果您需要错误列表,则不能使用此选项。

如果要创建错误消息列表,则可以使用the showErrors option。有关示例用法,请参阅the documentation

button

通用演示:http://jsfiddle.net/0k0vL1b0/

如果要取消每个字段旁边的消息,只需注释掉errorPlacement行并编写一些代码以从提供的showErrors: function(errorMap, errorList) { $("#summary").html("Your form contains " + this.numberOfInvalids() + " errors, see details below."); this.defaultShowErrors(); } 参数中提取消息。

DEMO 2:http://jsfiddle.net/0k0vL1b0/1/