JQuery/Javascript:是否启用复选框列表

JQuery/Javascript: to get checkboxlist enabled or not

本文关键字:启用 复选框 列表 是否 Javascript JQuery      更新时间:2023-09-26

启用或禁用这种控件没有问题,但是如果我只想知道状态怎么办?要知道其状态是启用还是禁用?

.prop("disabled",false);
.attr("disabled", true);

这些都是设置而不是获取

或者我什至尝试了一些类似的

$get("<%=cklst.ClientID %>").disabled;

不工作。

有什么提示吗?谢谢!

尝试:

$("#myid").prop("disabled")

Jquery 文档:http://api.jquery.com/prop/

根据您的示例,它可能是:

$("#<%=cklst.ClientID %>").prop("disabled")

请注意,若要设置禁用的属性,请使用:

$("#<%=cklst.ClientID %>").prop("disabled", true)


好的,所以你使用的是复选框列表,它略有不同。如果您查看从以下代码中发出的 html:

<asp:CheckBoxList ID="ck1" runat="server" Enabled="false"
RepeatDirection="Vertical" RepeatLayout="Table" EnableViewState="true" >
    <asp:ListItem>Item 1 Disabled</asp:ListItem>
         <asp:ListItem>Item 2 Disabled</asp:ListItem>
         <asp:ListItem>Item 3 Disabled</asp:ListItem>
         <asp:ListItem>Item 4 Disabled</asp:ListItem>
         <asp:ListItem>Item 5 Disabled</asp:ListItem>
         <asp:ListItem>Item 6 Disabled</asp:ListItem>
</asp:CheckBoxList>

你会看到它是这样的:

<table id="ctl00_cphMain_ck1" disabled="disabled" border="0">
    <tbody><tr>
        <td><span disabled="disabled"><input id="ctl00_cphMain_ck1_0" type="checkbox" name="ctl00$cphMain$ck1$0" disabled="disabled"><label for="ctl00_cphMain_ck1_0">Item 1 Disabled</label></span></td>
    </tr><tr>
        <td><span disabled="disabled"><input id="ctl00_cphMain_ck1_1" type="checkbox" name="ctl00$cphMain$ck1$1" disabled="disabled"><label for="ctl00_cphMain_ck1_1">Item 2 Disabled</label></span></td>
    </tr><tr>
        <td><span disabled="disabled"><input id="ctl00_cphMain_ck1_2" type="checkbox" name="ctl00$cphMain$ck1$2" disabled="disabled"><label for="ctl00_cphMain_ck1_2">Item 3 Disabled</label></span></td>
    </tr><tr>
        <td><span disabled="disabled"><input id="ctl00_cphMain_ck1_3" type="checkbox" name="ctl00$cphMain$ck1$3" disabled="disabled"><label for="ctl00_cphMain_ck1_3">Item 4 Disabled</label></span></td>
    </tr><tr>
        <td><span disabled="disabled"><input id="ctl00_cphMain_ck1_4" type="checkbox" name="ctl00$cphMain$ck1$4" disabled="disabled"><label for="ctl00_cphMain_ck1_4">Item 5 Disabled</label></span></td>
    </tr><tr>
        <td><span disabled="disabled"><input id="ctl00_cphMain_ck1_5" type="checkbox" name="ctl00$cphMain$ck1$5" disabled="disabled"><label for="ctl00_cphMain_ck1_5">Item 6 Disabled</label></span></td>
    </tr>
</tbody></table>

(现在,这可能会有所不同,具体取决于您为 ClientIDMode 和其他父控件 ID 设置的值类型。

ASP.NET 所做的是创建一个具有(有效地(CheckBoxList的单个ID的表html元素,然后在表中的每一行上,您都有一个html复选框元素,其ID已被枚举,例如ctl00_cphMain_ck1_0,ctl00_cphMain_ck1_1,ctl00_cphMain_ck1_2等。

这些复选框中的每一个都设置了一个禁用的属性。

因此,在

客户端,在处理复选框时,您必须更具体地针对它们。如果只使用 CheckBoxList ID,则会得到一个不正确的值,因为 JQuery 无法识别表元素上的禁用属性。因此,您需要查看一个(或多个(子复选框。

所以你可以做这样的事情:

$("#<%=cklst.ClientID %> input")[0].prop("disabled")

这实际上是在说:对于复选框列表 TABLE 元素,给我第一个输入子元素,并告诉我禁用的属性是什么。

有意义?