使用 jQuery 实现 radio 的选中与反选

使用 jQuery 实现 radio 的选中与反选

我们知道在 Html 中当我们选中一个radio后,再次点击该 radio,那么该 radio 依然是一个选中的状态,但是有时我们需要实现这样的逻辑:一个选中的 radio,再次点击该 radio 时,会取消它的选中状态 。

演示效果

实现逻辑

  • 取消 click 事件的默认动作
  • 使用 mouseup 控制 radio 的选中与反选
<!DOCTYPE HTML>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="http://www.jq22.com/jquery/jquery-1.10.2.js"></script>
        <script type="text/javascript">

            $(function(){

                $(":radio[name='sex']").click(function(event){
                    // 禁用事件的默认动作
                    event.preventDefault();
                })
                // 绑定一个鼠标单击事件
                .mouseup(function(){
                    $(this).prop("checked",!$(this).is(":checked"));
                });
            })
        </script>
  </head>
  <body>
    男:<input type="radio" name="sex" value="1"> 女:<input type="radio" name="sex" value="0">
 </body>
</html>

此文参考至:https://www.cnblogs.com/zz-930474270/p/7091147.html


转载于:https://www.cnblogs.com/duke-cui/p/11099113.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值