jQuery 有效与验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17404188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Valid vs Validate
提问by Akash Agrawal
I am trying to learn jQuery and have stumbled across a strange problem (perhaps strange only to me). So here goes:
I have a form with id mainform
in my app. Now I wanted to validate the form with jQuery. Also I wanted to set the invalid fields in blue color. For that I added css
我正在尝试学习 jQuery,但偶然发现了一个奇怪的问题(也许只有我觉得奇怪)。所以这里是:mainform
我的应用程序中有一个带有 id 的表单。现在我想用 jQuery 验证表单。我还想将无效字段设置为蓝色。为此,我添加了 css
.error{
background-color:blue;
}
When I run $('form#mainform').valid();
in the console, I get the highlights and everything works fine. However when I run $('form#mainform').validate();
, I get lots of data and nothing happens.
当我$('form#mainform').valid();
在控制台中运行时,我得到了亮点并且一切正常。但是,当我运行时$('form#mainform').validate();
,我得到了大量数据,但没有任何反应。
Also if I run .valid()
before validate()
, various options such as error placement etc. don't seem to work.
此外,如果我.valid()
之前运行validate()
,错误放置等各种选项似乎都不起作用。
I want to know the difference between these, why they are behaving so differently and where they should be used. I will appreciate if anyone can point me in right direction.
我想知道它们之间的区别,为什么它们的行为如此不同以及它们应该在哪里使用。如果有人能指出我正确的方向,我将不胜感激。
Note: I am using jquery.validate.js
注意:我正在使用 jquery.validate.js
回答by Dr Blowhard
There are several differences between valid and validate. Interestingly, although the docs state that
有效和验证之间有几个区别。有趣的是,尽管文档指出
"validate needs to be called on the form before checking it using this method"
“在使用此方法检查表单之前,需要在表单上调用验证”
this isn't actually the case, as valid calls validate() anyway.
实际情况并非如此,因为无论如何有效调用 validate() 。
The two major differences are
两个主要区别是
- If you want to pass options into the plugin, you must call validate({...})
- validate() doesn't highlight any errors, whereas valid() does. You could say that valid performs 'eager' validation whereas validate sets up a 'lazy' validation, basically if you call validate() you won't see any immediate change on the page, whereas with valid() you might.
valid can be called on a subset of form elements, whereas validate must be called on the form itself:
$('form').validate({/* options here */});
$('.myfields').valid()
- 如果要将选项传递给插件,则必须调用 validate({...})
- validate() 不会突出显示任何错误,而 valid() 会。您可以说 valid 执行“急切”验证,而 validate 设置“懒惰”验证,基本上如果您调用 validate() ,您将不会在页面上看到任何立即更改,而使用 valid() 您可能会看到。
valid 可以在表单元素的子集上调用,而 validate 必须在表单本身上调用:
$('form').validate({/* 这里的选项 */});
$('.myfields').valid()
回答by Suresh Atta
Assuming you are using Jquery validate library.
假设您使用的是 Jquery 验证库。
validate() needs to be called on the form before checking it using valid() method.
在使用 valid() 方法检查表单之前,需要在表单上调用 validate() 。
Not .valid
() before validate
()
不是 。valid
() 前validate
()
http://jqueryvalidation.org/valid/
http://jqueryvalidation.org/valid/