如何在jQuery验证中显示错误? [英] How can I show errors in jQuery validation?
问题描述
我们是否有任何函数在验证表单时返回所有错误消息?
Do we have any function which returns all the error messages while validating a form?
我尝试使用 defaultshowerros()函数但它返回当前正在验证的元素的错误消息。如何获取整个表单的所有错误消息?
I have tried using the defaultshowerros() function but it returns the error message for the element it is currently validating. How can I get all the error messages of the whole form?
推荐答案
如果存储对验证器的引用,例如:
If you store a reference to the validator, for example:
var validator = $("form").validate();
您可以拨打 .errors()
或任何时候 .invalidElements()
,例如:
You can call .errors()
or .invalidElements()
at any time on it, for example:
var errors = validator.errors(); //get the error elements, the actual labels
var errors = validator.invalidElements(); //the invalid elements themselves
如果您没有真正完成错误并希望它们出现在一次位置,使用内置的 errorLabelContainer
和包装
选项,例如:
If you're not really after the errors and just want them to appear in once place, use the built-in errorLabelContainer
and wrapper
options, for example:
<ul id="errors"></ul>
并参考:
$("form").validate({ errorLabelContainer: "#errors", wrapper: "li" });
您的错误将出现在该列表中,如果有/也会自动显示/隐藏没有任何错误。
And your errors would appear all in that list, which is also automatically shown/hidden if there are/aren't any errors.
这篇关于如何在jQuery验证中显示错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!