checkbox js验证只有一个可选

来源:互联网 发布:外文数据库免费入口 编辑:程序博客网 时间:2024/06/01 10:14

html提供两个选择框radio(单选且必选一个)、checkbox(多选可以不选)

 单选框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>
多选框checkbox实现只能选中一个

<div id="aaa">            <ul class="use_redpackets_nav">            <li>            <input type="checkbox" name="oneBox" value=""/>                </li>               <li>            <input type="checkbox" name="oneBox" value=""/>                </li>            </ul></div><script type="text/javascript">$(function () {            var aaa= $("#aaa input:checkbox");            aaa.click(function () {               if(this.checked || this.checked=='checked'){                   aaa.removeAttr("checked");                   //这里需注意jquery1.6以后必须用prop()方法                   //$(this).attr("checked",true);                   $(this).prop("checked", true);                 }            });         });   </script>
//div下的checkbox查看选中个数
<script type="text/javascript">function  checkedValue(){     var num=0;     var aaa=$("#aaa input:checkbox");     aaa.each(function(){     if ($(this).is(':checked')) {        alert($(this).val());                num++;            }       alert(num);          });     }   </script>

原创粉丝点击