jquery单选框redio设置checked属性无效的原因

jquery设置标签本身的属性使用prop(),设置自定义属性使用attr()。

下面用例:点击选中复选框,默认选择单选框的“是”,取消复选框之后,取消单选框的选择。

<div style="border:1px solid #ddd;padding-bottom: 20px;">
    <h5>jquery控制,</h5>
    <small style="display: block;">当复选框选中时,默认选中auth中的‘是’,复选框不选中,单选框全部不选中</small>

    <label><input type="checkbox" name="aa" class="btn1"> 点击我</label>
    <br>
    <label><input type="radio" name="auth" value="1"></label>
    <label><input type="radio" name="auth" value="0"></label>
</div>
<script src="https://s3.pstatp.com/cdn/expire-1-M/jquery/3.0.0/jquery.min.js"></script>
<!-- <script src="https://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script> -->
<script>
    $(".btn1").click(function () {
        if (this.checked) {
            //选中 auth值为1的单选框
            $("input[name='auth']").each(function () {
                if ($(this).val() === '1') {
                    // 1. 高版本jquery
                    // $(this).attr('checked', true);

                    //2.高版本jquery和低版本
                    $(this).prop('checked', true);

                    //3.原生js
                    // this.checked = true;
                }
            });
        } else {
            //取消auth所有选中的值
            $("input[name='auth']").each(function () {
                // 1.高版本jquery
                // $(this).attr('checked', false);

                //2.高版本jquery和低版本
                $(this).prop('checked', false);

                //3.原生js
                // this.checked = false;
            });
        }
    });
</script>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值