【问题标题】:Disabling other items in a checkboxlist not working禁用复选框列表中的其他项目不起作用
【发布时间】:2014-02-14 02:56:20
【问题描述】:

我正在尝试使用此代码禁用我的 aspcheckbox 列表中的其他选项(不是复选框,这是 asp.net checkboxlist 控件)...

这里是jquery

$(document).ready(function () {
    $('#<%=lstExposureValue.ClientID%> input:checkbox').click(function () {

        var currentIdone = 'Unknown';
        var checked = false;
        $('#<%=lstExposureValue.ClientID%> input:checkbox').each(function () {

            var currentId = $(this).next().html();
            if (currentId == currentIdone) {
                if (checked) {
                    $(this).prop('enabled', true);
                    $("#<%=lstExposureValue.ClientID%> input:checkbox:not(:checked)").prop("enabled", true);
                    checked = false;
                    return;
                }
                else {
                    $("#<%=lstExposureValue.ClientID%> input:checkbox:not(:checked)").prop("disabled", true);
                    $("#<%=lstExposureValue.ClientID%> input:checkbox:not(:checked)").prop("checked", false);
                    checked = true;
                }
            }
        });

    });

});

这里是asp.net

          <h3>
            One</h3>
         <asp:CheckBoxList ID="lstExposureValue" runat="server">
            <asp:ListItem>Short-term exposure</asp:ListItem>
             <asp:ListItem>Medium-term exposure</asp:ListItem>
             <asp:ListItem>Unknown</asp:ListItem>
          </asp:CheckBoxList>

so when unknown is selected the other 3 options has to be unselected and disabled. 如果未选中未知,则应启用所有选项。

现在无论我选择哪个选项,它都会禁用其他选项,当我取消选中相同的选项时,它会禁用所有选项,任何人都可以帮忙.....

【问题讨论】:

    标签: javascript jquery asp.net checkboxlist


    【解决方案1】:
    $("#<%=lstExposureValue.ClientID%> input:checkbox:not(:checked)").prop("disabled", true);
    $("#<%=lstExposureValue.ClientID%> input:checkbox:not(:checked)").prop("checked", false);
    

    您不能更改禁用元素的状态。首先检查它,然后禁用该元素。

    【讨论】:

    • 如何在代码中出现任何错误。我应该在哪里进行更正
    【解决方案2】:

    这就是你的做法。

    $(document).ready(function () {
        var checked = false;
        $('#<%=lstExposureValue.ClientID%> input:checkbox').click(function () {
            var currentIdone = 'Unknown';
            var currentId = $(this).next().html();
            if (currentId == currentIdone) {
    
                if (checked) {
    
                    $("#<%=lstExposureValue.ClientID%> input").removeAttr('disabled');
                    checked = false;
                    return;
                }
                else {
                    $("#<%=lstExposureValue.ClientID%> input").attr('checked', false);
                    $(this).attr('checked', true);
                    $('#<%=lstExposureValue.ClientID%> input:not(:checked)').attr('disabled', 'disabled');
                    checked = true;
                }
    
    
            }
    
        });
    });
    

    有效

    【讨论】:

    • 'checked' 和 'disabled' 是属性,而不是属性,最好使用 '.prop' 处理
    猜你喜欢
    • 2015-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-20
    相关资源
    最近更新 更多