javascript 如何使用 jQuery 验证插件检查特定字段是否有效?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30048141/
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
How to check specific field is valid or not using jQuery validation plugin?
提问by Manan
I am trying to check specific form field is valid or not on button click event but not working.
我正在尝试在按钮单击事件中检查特定表单字段是否有效但不起作用。
I have included this jQuery library:
我已经包含了这个 jQuery 库:
<script type='text/javascript' src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.8/jquery.validate.min.js"></script>
and jQuery code:
和 jQuery 代码:
<script type='text/javascript'>//<

Any idea why it is not working.
知道为什么它不起作用。
Thanks.
谢谢。
采纳答案by Shaunak D
There is no element in your code with ID as brokercat, #brokercatis an ID based selector. It is the nameattribute.
您的代码中没有 ID 为 的元素brokercat,它#brokercat是基于 ID 的选择器。它是name属性。
Use
利用
$("[name='brokercat']").valid() //returns 0 or 1
$(".next").click(function () {
alert($("[name='brokercat']").valid());
});
*Note : Make sure you include both jQuery and jQuery.validate.js references which are compatible.
*注意:确保包含兼容的 jQuery 和 jQuery.validate.js 引用。
回答by vinayakj
You are using the nameproperty twice on same element.
Change below
您name在同一元素上两次使用该属性。下面更改
<input name="brokercat" id="radiobtn" type="radio" name="radio" value="cat05">
<input name="brokercat" id="radiobtn" type="radio" name="radio" value="cat05">
to
到
<input name="brokercat" id="radiobtn" type="radio" value="cat05">
<input name="brokercat" id="radiobtn" type="radio" value="cat05">

