扩展 Jquery 验证以在验证运行后显示/隐藏 div 或清除 div

发布于 2024-08-30 08:00:28 字数 201 浏览 2 评论 0原文

为了澄清,如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

芯好空 2024-09-06 08:00:28

有 2 个属性可以直接执行此操作 errorContainererrorLabelContainer,例如您想要

中的错误列表和

根据是否存在隐藏/显示,您可以这样做:
$("#myform").validate({
   errorContainer: "#myDivID",
   errorLabelContainer: "#myDivID ul",
   wrapper: "li",
   //other options/rules...
});

选项页面对此有更多详细信息,但这就是简单修复所需的一切,假设您拥有一个列表,从我不完全确定的问题来看。如果您想在 div 中添加 div,不用担心...只需将 "#myDivID ul" 更改为 "#myDivID""li""div"...或任何您想要的元素。

如果没有错误,上面将隐藏

,如果有错误则显示它。即使您在 div 顶部有描述/帮助消息或其他内容,并且错误只是 div 的一部分(由 errorLabelContainer 选择器确定),这仍然有效...它仍然会隐藏/根据任何错误进行显示,并保持您的消息/帮助不变。

There are 2 properties to do this directly errorContainer and errorLabelContainer, 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:

$("#myform").validate({
   errorContainer: "#myDivID",
   errorLabelContainer: "#myDivID ul",
   wrapper: "li",
   //other options/rules...
});

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 the errorLabelContainer selector...it'll still hide/show based on any errors and leave your message/help untouched.

可爱暴击 2024-09-06 08:00:28

您可以附加许多事件处理程序。 成功 & 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, but errorPlacement might also be useful.

See http://docs.jquery.com/Plugins/Validation/validate#toptions for more information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文