为什么jQuery Validate插件无需调用validate()即可进行验证? [英] Why does jQuery Validate plugin do validation without calling validate()?

查看:132
本文介绍了为什么jQuery Validate插件无需调用validate()即可进行验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是给了validate选项(errorElement,...),我设置了规则,没有调用validate方法,也没有将它与任何事件相关联,但是jQuery自动进行验证,如何控制验证时间?

I have just give option to the validate (errorElement, ...), and I set the rules, without calling validate method, or associating it with any event, but jQuery validates automatically, how to control when it validates?

推荐答案

您将需要提供有关您要执行的操作的更多详细信息,但是在您编写该问题时,我可以回答您的问题到目前为止.

You're going to need to provide a lot more detail about what you're trying to do, but I can answer your question as you've written it so far.

标题:为什么jquery在不调用validate的情况下进行验证?

Title: why does jquery validate without calling validate?

.validate()被称为一次以初始化插件表格.它不是触发按需验证的方法.初始化后,表单将根据插件中内置的各种事件回调自动进行验证.接下来是更多详细信息...

.validate() is called once to initialize the plugin on the form. It is not a method for triggering validation on demand. Once initialized, the form will automatically validate as per the various event callbacks built into the plugin. More details follow...

OP :我刚刚给了validate选项(errorElement,...),我设置了 规则,而无需调用validate方法或将其与任何方法相关联 事件,但jquery自动验证,如何控制其时间 验证?

OP: i have just give option to the validate (errorElement, ...), and i set the rules, without calling validate method, or associating it with any event, but jquery validates automatically, how to control when it validates ?

您不需要将插件与任何事件相关联,因为插件已经自动执行此操作.如果没有的话,那将是毫无用处的.

You do not need to associate the plugin with any events because the plugin already does this automatically. It would be pretty useless if it didn't.

调用.validate()时,您正在初始化表单上的插件.因此,是的,这意味着表单现在已经准备好,它将自动执行针对所有常见事件(例如submitonkeyuponfocusout)并选择,选择按钮&的触发的验证测试.复选框click.你为什么不想那样?

When you call .validate(), you are initializing the plugin on the form. So, yes, this means the form is now ready and it will automatically do a validation test triggered on all common events such as submit, onkeyup, onfocusout, and select, button & checkbox click. Why would you not want that?

要以编程方式执行验证测试,可以调用内置的.valid()方法非常类似于submit事件,因为它将触发整个表单上的验证测试,并且该表单将显示任何错误. .valid()还将返回布尔值truefalse,指示表单是否通过验证测试.

To perform a validation test programmatically, you can call the built-in .valid() method which is very much like the submit event in that it will trigger the validation test on the entire form and the form will display any errors. .valid() will also return a boolean true or false indicating if the form passes the validation test.

如果仅需要触发单个元素而不是整个表单的验证测试,则可以使用$('#myelement').valid().再次显示任何消息,并返回布尔值.

If you need to trigger a validation test only on a single element, rather than the entire form, you would use $('#myelement').valid(). Again, any message is displayed and a boolean value returned.

如果出于任何原因需要完全禁用"和启用"验证,则不能直接执行此操作,因为没有内置方法可以将插件从表单中解除绑定.但是,您可以使用内置的rules('remove')方法.在任何字段上都没有任何规则,它的行为就好像根本没有验证一样.但是,您仍然可以使用其他插件选项,例如 submitHandler回调,以防万一您使用ajax提交数据.

If for whatever reason you need to actually "disable" and "enable" validation completely, you can not directly do this, as there is no built-in method for un-binding the plugin from the form. However, you can simulate it by using the built-in rules('remove') and rules('add') methods. Without any rules on any fields, it will simply behave as if there's no validation at all. However, you would still be able to use other plugin options like the submitHandler callback, just in case you are using ajax to do the data submission.

这篇关于为什么jQuery Validate插件无需调用validate()即可进行验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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