使用JQuery验证的showErrors()函数,我将错误手动添加到表单中:
var errors = {};
errors['Field1'] = 'Field1 has an error';
errors['Field2'] = 'Field2 has an error';
errors['Field3'] = 'Field3 has an error';
$('form').validate().showErrors(errors);
为什么我不能再使用删除它们$('form').validate().resetForm();
?这样做的唯一方法似乎是将所有索引重置为null:
var errors = {};
errors['Field1'] = null;
errors['Field2'] = null;
errors['Field3'] = null;
$('form').validate().showErrors(errors);
使用showErrors()
回调函数时,使用此回调函数进行的任何手动操作,还必须手动“撤消”。
该.resetForm()
方法仅通过清除字段的无效状态来重置验证插件。由于默认错误消息是由插件自动处理的,因此,这些错误将因resetForm()
调用而消失。
由于您没有使用自动生成的错误,因此这resetForm()
对您的自定义消息摘要无效。
但是,还有一个称为的选项errorContainer
会与错误一起切换。如果将错误摘要放入此容器中,它将自动与错误一起切换。
HTML:
<div id="summary"></div>
jQuery的:
$(document).ready(function () {
$('#myform').validate({
// other rules and options,
errorContainer: "#summary", // will toggle along with errors
showErrors: function (errorMap, errorList) {
// your custom function
}
});
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句