我们有没有在验证表单时返回所有错误消息的函数?
我已尝试使用 defaultshowerros() 函数,但它返回当前正在验证的元素的错误消息。如何获取整个表单的所有错误信息?
最佳答案
如果您存储对验证器的引用,例如:
var validator = $("form").validate();
您可以随时在其上调用.errors()
或.invalidElements()
,例如:
var errors = validator.errors(); //get the error elements, the actual labels
var errors = validator.invalidElements(); //the invalid elements themselves
如果您不是真的在寻找错误,只是想让它们出现在一个地方,请使用内置的 errorLabelContainer
and wrapper
options ,例如:
<ul id="errors"></ul>
并引用:
$("form").validate({ errorLabelContainer: "#errors", wrapper: "li" });
并且您的错误将全部出现在该列表中,如果有/没有任何错误,该列表也会自动显示/隐藏。
关于javascript - 如何在 jQuery 验证中显示错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3811044/