javascript jQuery 验证只验证一个字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5971392/
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
jQuery validate only validates one field
提问by Andrew Boes
I am using jQuery validate to validate a form. I have two text boxes on my form and only the first one will add the "This field is required." message. If I remove the "required" class from the first one the second will have the message.
我正在使用 jQuery 验证来验证表单。我的表单上有两个文本框,只有第一个会添加“此字段为必填字段”。信息。如果我从第一个中删除“必需”类,第二个将收到消息。
html:
html:
<form id="questionForm">
<div><input class="required" id="value" type="text" /></div>
<div><textarea class="required" id="description"></textarea></div>
<button type="submit">Save</button>
</form>
javascript:
javascript:
$("#questionForm").validate({ submitHandler: function() {
alert("valid");
}
});
Why is only one being validated?
为什么只有一个被验证?
Edit: I'm using jQuery validation plug-in 1.7
编辑:我正在使用 jQuery 验证插件 1.7
- http://bassistance.de/jquery-plugins/jquery-plugin-validation/
- http://docs.jquery.com/Plugins/Validation
- http://bassistance.de/jquery-plugins/jquery-plugin-validation/
- http://docs.jquery.com/Plugins/Validation
Edit 2: I'm using MVC 3
编辑 2:我正在使用 MVC 3
回答by Jay
I just ran into this issue myself. Pulled my hair out for an hour, but here it is:
我自己刚刚遇到了这个问题。把我的头发拉了一个小时,但这里是:
Throw the "name" attribute in there as well and that may do the trick.
将“名称”属性也扔在那里,这可能会奏效。
回答by Sulabh Singla
You have to enter the name
attribute for every DOM element which you want to validate using the Jquery.validate()
function .
您必须为name
要使用Jquery.validate()
函数验证的每个 DOM 元素输入属性 。
回答by Oliver Spryn
Without knowing what validation plugin you are using, it is difficult to nail down the problem. However, it seems as though jQuery isn't loop through each of the fields, but it stops when it reaches in an invalid field. Try using the jQuery $.each()
method, and using some conditionals to make sure the validation process does not stop when the validator reaches an invalid field.
在不知道您使用的是什么验证插件的情况下,很难确定问题所在。但是,似乎 jQuery 并没有遍历每个字段,而是在到达无效字段时停止。尝试使用 jQuery$.each()
方法,并使用一些条件来确保验证过程在验证器到达无效字段时不会停止。
Hope that helps,
spryno724
希望有帮助,
spryno724
回答by Joshua - Pendo
I think the alert is blocking the validation of both fields. After 1 alert the rest of the javascript stops. What happens if you use $('#questionForm').validate();
instead?
我认为警报阻止了两个字段的验证。1 警报后,其余的 javascript 停止。如果你改用会发生什么$('#questionForm').validate();
?