扩展 Jquery 验证以在验证运行后显示/隐藏 div 或清除 div
为了澄清,如何在 jquery 的验证插件运行其验证函数之前运行自定义函数(这是查看所有字段并显示错误消息的函数)?
现在我将所有错误输出到 div 中。我想捕获验证事件并在运行我的旧错误之前清除 div。然后我喜欢根据它是否有任何由jquery验证插件自动生成的标签来显示/隐藏该div。我可以做一切事情,但不知道如何扩展事件,运行我需要的东西,然后调用验证函数。谢谢!
To clarify, how can run a custom function right before jquery's validation plugin runs its validate function (Which is what looks at all the fields and shows the error message)?
Right now i have all of my errors outputting to a div. Id like to catch the validate event and clear the div right before its ran of my old errors. Then id like to show/hide that div based on if it has any labels in it that were auto generated by the jquery validation plugin. I can do everything but figure out how to extend the event, run the stuff i need to, then call the validate function. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有 2 个属性可以直接执行此操作
errorContainer
和errorLabelContainer
,例如您想要中的错误列表和
根据是否存在隐藏/显示,您可以这样做:
选项页面对此有更多详细信息,但这就是简单修复所需的一切,假设您拥有一个列表,从我不完全确定的问题来看。如果您想在 div 中添加 div,不用担心...只需将
"#myDivID ul"
更改为"#myDivID"
和"li"
到"div"
...或任何您想要的元素。如果没有错误,上面将隐藏
,如果有错误则显示它。即使您在 div 顶部有描述/帮助消息或其他内容,并且错误只是 div 的一部分(由
errorLabelContainer
选择器确定),这仍然有效...它仍然会隐藏/根据任何错误进行显示,并保持您的消息/帮助不变。There are 2 properties to do this directly
errorContainer
anderrorLabelContainer
, for example of you wanted a list of the errors in your<div>
and the<div>
to hide/show based on if any are present, you'd do this:The options page has more detail around this, but that's everything you need for a simple fix, assuming a list is what you have, from the question I wasn't entirely sure. If you wanted say divs inside your div, no sweat...just change
"#myDivID ul"
to"#myDivID"
and"li"
to"div"
...or whatever element you want.The above will hide
<div id="myDivID">
if there are no errors, show it if there are any. This works even if you have a description/help message or something at the top of the div and the errors are just a portion of the div, determined by theerrorLabelContainer
selector...it'll still hide/show based on any errors and leave your message/help untouched.您可以附加许多事件处理程序。
成功
&invalidHandler
似乎最适合您的要求,但errorPlacement
可能也很有用。请参阅http://docs.jquery.com/Plugins/Validation/validate#toptions 了解更多信息。
There are a number of event handlers that you can attach to.
success
&invalidHandler
seem to be most appropriate for your requirements, buterrorPlacement
might also be useful.See http://docs.jquery.com/Plugins/Validation/validate#toptions for more information.