javascript – 如何使用jQuery验证插件检查特定字段是否有效?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 如何使用jQuery验证插件检查特定字段是否有效?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3040字,纯文字阅读大概需要5分钟。
内容图文
![javascript – 如何使用jQuery验证插件检查特定字段是否有效?](/upload/InfoBanner/zyjiaocheng/795/8eb1596351bf48bda96478ffe9c2c109.jpg)
参见英文答案 > jQuery Validation plugin: how to check if an element is valid? 2个
我试图在按钮点击事件上检查特定表单字段是否有效但不起作用.
我已经包含了这个jQuery库:
<script type='text/javascript' src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.8/jquery.validate.min.js"></script>
和jQuery代码:
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$(function () {
//The client side validations
var validator = $("#form").validate({
rules: {
brokercat: {
required: true
}
},
messages: {
brokercat: {
required: "required"
}
},
});
});
$(".next").click(function () {
alert($("#brokercat").valid());
});
});
</script>
HTML代码:
<form name="form" id="form" action="#">
<div class="radio">
<input name="brokercat" id="radiobtn" type="radio" name="radio" value="cat05">
<label for="radiobtn">
<p>My business would benefit from having more cash available.</p>
</label>
<input name="brokercat" id="radiobtn1" type="radio" name="radio" value="cat04">
<label for="radiobtn1">
<p>My business needs to buy a new piece of equipment.</p>
</label>
<input name="brokercat" id="radiobtn2" type="radio" name="radio" value="cat02">
<label for="radiobtn2">
<p>My business wants to buy the property that it already
<br/>occupies or is looking to move into. Or, I am looking to buy
<br/>a business and that includes the premises it occupies.</p>
</label>
<input name="brokercat" id="radiobtn3" type="radio" name="radio" value="cat06">
<label for="radiobtn3">
<p>I need to acquire a new vehicle for my business.</p>
</label>
<input name="brokercat" id="radiobtn4" type="radio" name="radio" value="cat07">
<label for="radiobtn4">
<p>I am looking to develop residential or commercial premises.</p>
</label>
<input name="brokercat" id="radiobtn5" type="radio" name="radio" value="cat08">
<label for="radiobtn5">
<p>I need to borrow for less than 12 months for a property transaction.</p>
</label>
<input name="brokercat" id="radiobtn6" type="radio" name="radio" value="cat03">
<label for="radiobtn6">
<p>I have outstanding invoices that I would like to use to raise
<br/>some extra funding.</p>
</label>
<input name="brokercat" id="radiobtn7" type="radio" name="radio" value="cat09">
<label for="radiobtn7">
<p>I/we have a pension fund that we would like to consider
<br/>using to raise some capital.</p>
</label>
<input name="brokercat" id="radiobtn8" type="radio" name="radio" value="cat01">
<label for="radiobtn8">
<p>I am looking to buy a property as an investment and to rent
<br/>it out.</p>
</label>
<input name="brokercat" id="radiobtn9" type="radio" name="radio" value="cat10">
<label for="radiobtn9">
<p>My business is looking to help with its overseas trading and
<br/>exporting.</p>
</label>
<input type="button" class="next" value="Next" />
</div>
</form>
我收到此错误:
知道为什么它不起作用.
谢谢.
解决方法:
您的代码中没有ID为brokercat的元素,#drkercat是基于ID的选择器.它是name属性.
使用
$("[name='brokercat']").valid() //returns 0 or 1
$(".next").click(function () {
alert($("[name='brokercat']").valid());
});
*注意:确保包含兼容的jQuery和jQuery.validate.js引用.
内容总结
以上是互联网集市为您收集整理的javascript – 如何使用jQuery验证插件检查特定字段是否有效?全部内容,希望文章能够帮你解决javascript – 如何使用jQuery验证插件检查特定字段是否有效?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。