复选框当单选按钮使用

    平时会遇到本来是单选的项,用radio是可以进行单选,但需要取消选中项时,除非自己代码进行控制不然无法取消选中,在这种前提下个人更倾向使用checkbox来实现单选。故特地分享下checkbox实现单选的js代码。
1.加入如下js代码:
<script type="text/javascript">
//复选框单选
$("td[isOnly='only']").find(":checkbox").each(function(){
$(this).click(function(){
  if($(this).attr("checked")){
   $(this).parent().find(":checkbox").removeAttr('checked');
   $(this).attr('checked','checked');
  }
});
});
</script>

 

<td isOnly="only">

     <input type="checkbox" id="box1"><label for='box1'>测试1</label>

<input type="checkbox" id="box2"><label for='box2'>测试2</label>

</td>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值