jQuery --- 实现 checkbox 样式的单选框

这是之前写的一个模态框 要求单选 但是 要求radio的默认样式 太难看了 就想用checkbox + js 自己把它改成单选框 如下:

  html代码:

复制代码
<div class="list-item">
    <input type="checkbox" id="1" value="">
    <label for="1">Dance of the Reed Pipes</label>
</div>
<div class="list-item">
    <input type="checkbox" id="2" value="">
    <label for="2">Dance of the Reed Pies</label>
</div>
<div class="list-item">
    <input type="checkbox" id="3" value="">
    <label for="3">Dance of the Reed Pipes</label>
</div>
<div class="list-item">
    <input type="checkbox" id="4" value="">
    <label for="4">Dance of the Reed Pipes</label>
</div>
<div class="list-item">
    <input type="checkbox" id="5" value="">
    <label for="5">Dance of the Reed Pipes</label>
</div>
复制代码

  js代码:

复制代码
$(".list-item").click(function(){
     $(".list-item").find("input[type='checkbox']").prop("checked", false);//每次点击前,将所有checkbox置为 未选中
     var cobj = $(this).find("input[type='checkbox']");//当前点击的checkbox
     cobj.prop("checked", true);//将当前点击的checkbox置为选中状态
        //over
})
复制代码

  代码并不复杂 挺简单的 ,如果想获取点击的id,只要再加一句 

var itemId = cobj.attr("id");

  这样就可以了 

转载于:https://www.cnblogs.com/amylis_chen/p/11520560.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值