一旦字段得到验证,我就很难从 errorPlacement 指定的区域中删除错误。非常感谢任何帮助!
$('document').ready(function(){
$('form').validate({
rules: {
a: {required:true, minlength:2}
},
messages: {
a: {required: "enter your name!"}
},
errorPlacement: function(error, element) {
if(element.attr('name') == 'a'){
error.appendTo($('#restErrorDate'));
}
},
success: function(error){
},
debug:true
});
$('#a').blur(function(){
$("form").validate().element("#a");
});
});
这是 html:
<div>
<form action="#" id='commentForm'>
<input type="text" name="a" id="a">
</form>
</div>
<div id="restErrorDate" class="restErrorDate" style="border:1px solid blue;"></div>
这是 jsfiddle:
最佳答案
不用追加(这会不断添加更多内容),只需覆盖所有现有的 HTML:
errorPlacement: function(error, element) {
if(element.attr('name') == 'a'){
// error.appendTo($('#restErrorDate'));
$('#restErrorDate').html(error);
}
},
关于jQuery 验证 errorPlacement 删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13535731/