checkbox的只能选中一个,div下checkbox的遍历
2016-02-23 12:08
651 查看
html提供两个选择框radio(单选且必选一个)、checkbox(多选可以不选),但是我想实现一个只能选中一个时且可以不选时的功能时,会发现两个功能都不好用,这是后就需要自己写代码控制了。我觉得有两种方案
1 单选框radio可取消选中
2 多选框checkbox实现只能选中一个
3遍历div下的checkbox查看选中个数
1 单选框radio可取消选中
<input type="radio" name="aa" onclick="if(this.c==1){this.c=0;this.checked=0}else this.c=1" c="0"> jquery方法 <input type="radio" name="aa" onclick="chk();"> <script language=javascript> function chk() { document.all.aa.checked=false; } </script>
2 多选框checkbox实现只能选中一个
<div class="use_redpackets_content" id="oneCheck"> <ul class="use_redpackets_nav"> <li> <input type="checkbox" name="oneBox" value=""/> </li> <li> <input type="checkbox" name="oneBox" value=""/> </li> <li> <input type="checkbox" name="oneBox" value=""/> </li> </ul> </div> <script type="text/javascript"> $(function () { var fanxiBox = $("#oneCheck input:checkbox"); fanxiBox.click(function () { if(this.checked || this.checked=='checked'){ fanxiBox.removeAttr("checked"); //这里需注意jquery1.6以后必须用prop()方法 //$(this).attr("checked",true); $(this).prop("checked", true); } }); }); </script>
3遍历div下的checkbox查看选中个数
<div class="use_redpackets_content" id="oneCheck"> <ul class="use_redpackets_nav"> <li> <input type="checkbox" name="oneBox" value=""/> </li> <li> <input type="checkbox" name="oneBox" value=""/> </li> <li> <input type="checkbox" name="oneBox" value=""/> </li> </ul> </div> <script type="text/javascript"> function checkedValue< 4000 span class="hljs-params">(){ var num=0; var fanxiBox=$("#oneCheck input:checkbox"); fanxiBox.each(function(){ if ($(this).is(':checked')) { alert($(this).val()); num++; } alert(num); }); } </script>
相关文章推荐
- JQuery1——基础($对象,选择器,对象转换)
- JavaScript演示排序算法
- 2015-2016网页设计趋势分析 Web Design of Trends
- jQuery Ajax 跨域调用
- 移动端的长按事件
- jquery教程靠边站,一分钱不花让你免费学会jquery
- JQuery+Strusts1.x无刷新登录
- 在Windows 8.1的IE 11中屏蔽双击放大功能
- JavaScript 各种遍历方式详解
- 数组方法汇总
- 文件遍历排序函数
- 通过Mootools 1.2来操纵HTML DOM元素
- jQuery菜单插件用法实例
- JQuery 初体验(建议学习jquery)
- WEB标准网页布局中尽量不要使用的HTML标签
- 基于Jquery和CSS3制作数字时钟附源码下载(CSS3篇)
- Jquery实现的table最后一行添加样式的代码
- jQuery实现向下滑出的平滑下拉菜单效果
- jQuery 练习[一] 学习jquery的准备工作