验证运行后扩展Jquery validate以显示/隐藏div或清除div [英] Extending Jquery validate to show/hide div or clear div after validate is ran

查看:265
本文介绍了验证运行后扩展Jquery validate以显示/隐藏div或清除div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了澄清,如何在jquery的验证插件运行其validate函数之前运行自定义函数(这是在所有字段中查看并显示错误消息的原因)?

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)?

现在我将所有错误输出到div.我想赶上validate事件并在div跑掉我的旧错误之前清除div.然后id喜欢显示/隐藏该div,基于它是否具有由jquery验证插件自动生成的标签.我可以做所有事情,但要弄清楚如何扩展事件,运行所需的东西,然后调用validate函数.谢谢!

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!

推荐答案

有两个属性可以直接errorContainererrorLabelContainer进行此操作,例如,您需要一个<div>中的错误列表以及<div>要根据是否存在隐藏/显示,请执行以下操作:

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...
});

选项页面对此有更多详细信息,但这就是您所需要的一切一个简单的修复程序,假设您拥有列表,那么我就不能完全确定这个问题.如果您想在div内说出div,不要费劲...只需将"#myDivID ul"更改为"#myDivID",将"li"更改为"div" ...或您想要的任何元素.

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.

如果没有错误,则以上内容将隐藏<div id="myDivID">,如果存在错误,则将其显示.即使您在div顶部有描述/帮助消息或其他内容,并且错误只是div的一部分(由errorLabelContainer选择器确定),此方法仍然有效...它仍会基于任何内容隐藏/显示错误,并保持您的消息/帮助不变.

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.

这篇关于验证运行后扩展Jquery validate以显示/隐藏div或清除div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆