关于Jquery反复选中单选框无效问题

需求:系统默认选项为“是”的情况下,不能够编辑并且不能够删除。也就是说当选择选“系统默认”项的值为“是”时,“允许编辑”和“允许删除”项的值默认为“否”。

问题原因:在点击不同的“系统默认”单选按钮选项的时候,在change事件中根据当前控件选中值,改变“允许编辑”和“允许删除”项的单选框默认选中值,反复操作,每个单选框只有第一次选择才会有效。

Html代码如下:

                    <div class="form-group row">
                            <label class="col-sm-2 col-form-label text-right">系统默认:</label>
                            <div class="col-sm-10 row m-auto">
                                <div class="radio radio-success">
                                    <input type="radio" id="IsSystem_1" value="1" name="IsSystem">
                                    <label for="IsSystem_1">是</label>
                                </div>
                                <div class="radio radio-danger">
                                    <input type="radio" id="IsSystem_0" value="0" name="IsSystem" checked>
                                    <label for="IsSystem_0">否</label>
                                </div>
                            </div>
                        </div>
                        <div class="form-group row">
                            <label class="col-sm-2 col-form-label text-right">允许编辑:</label>
                            <div class="col-sm-4 row m-auto">
                                <div class="radio radio-success">
                                    <input type="radio" id="AllowEdit_1" value="1" name="AllowEdit" checked>
                                    <label for="AllowEdit_1">是</label>
                                </div>
                                <div class="radio radio-danger">
                                    <input type="radio" id="AllowEdit_0" value="0" name="AllowEdit">
                                    <label for="AllowEdit_0">否</label>
                                </div>
                            </div>
                            <label class="col-sm-2 col-form-label text-right">允许删除:</label>
                            <div class="col-sm-4 row m-auto">
                                <div class="radio radio-success">
                                    <input type="radio" id="AllowDelete_1" value="1" name="AllowDelete" checked>
                                    <label for="AllowDelete_1">是</label>
                                </div>
                                <div class="radio radio-danger">
                                    <input type="radio" id="AllowDelete_0" value="0" name="AllowDelete">
                                    <label for="AllowDelete_0">否</label>
                                </div>
                            </div>
                        </div>

JS代码如下:

            //系统默认选择是的话,不允许删除,不允许编辑
            $("input:radio[name='IsSystem']").change(function(){
                var val = $('input:radio[name="IsSystem"]:checked').val();
                if (val == "1") {
                    $("input:radio[name='AllowEdit'][value='0']").attr("checked",true);
                    $("input:radio[name='AllowDelete'][value='0']").attr("checked", true);
                }else{
                    $("input:radio[name='AllowEdit'][value='1']").attr("checked", true);
                    $("input:radio[name='AllowDelete'][value='1']").attr("checked", true);
                }
            });

效果图如下:

解决方法:每次Jquery赋值都需要控制同一name的其他值的属性。

修改后的JS代码如下:

            //系统默认选择是的话,不允许删除,不允许编辑
            $("input:radio[name='IsSystem']").change(function(){
                var val = $('input:radio[name="IsSystem"]:checked').val();
                if (val == "1") {
                    $("input:radio[name='AllowEdit'][value='1']").removeAttr("checked");
                    $("input:radio[name='AllowDelete'][value='1']").removeAttr("checked");
                    $("input:radio[name='AllowEdit'][value='0']").attr("checked",true);
                    $("input:radio[name='AllowDelete'][value='0']").attr("checked", true);
                    $("input:radio[name='AllowEdit']").attr("disabled", true);
                    $("input:radio[name='AllowDelete']").attr("disabled", true);
                }else{

                    $("input:radio[name='AllowEdit']").removeAttr("disabled");
                    $("input:radio[name='AllowDelete']").removeAttr("disabled");
                    $("input:radio[name='AllowEdit'][value='0']").removeAttr("checked");
                    $("input:radio[name='AllowDelete'][value='0']").removeAttr("checked");
                    $("input:radio[name='AllowEdit'][value='1']").attr("checked", true);
                    $("input:radio[name='AllowDelete'][value='1']").attr("checked", true);
                }
            });

效果图如下:

问题已解决。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值