天天看点

关于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反复选中单选框无效问题

解决方法:每次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);
                }
            });
           

效果图如下:

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

问题已解决。