简体   繁体   English

如何通过jQuery禁用asp.net单选按钮列表

[英]How to disable asp.net radiobuttonlist via jQuery

I have a checkbox and radiobuttonlist defined as follows: 我有一个复选框和单选按钮列表,定义如下:

<asp:CheckBox id="chkChange" runat="server" text="Enable" />
<br />
<asp:RadioButtonList id="rblConsole" runat="server" cssclass="console">
    <asp:ListItem text="XBox 360" value="xbox" />
    <asp:ListItem text="Playstation 3" value="playstation" />
</asp:RadioButtonList>

These controls are in a content page with a master page so the actual html rendered is: 这些控件位于具有母版页的内容页中,因此呈现的实际html是:

<table id="ctl00_ContentPlaceHolder1_rblConsole" class="console" border="0">
    <tr>
        <td><input id="ctl00_ContentPlaceHolder1_rblConsole_0" type="radio" name="ctl00$ContentPlaceHolder1$rblConsole" value="xbox" /><label for="ctl00_ContentPlaceHolder1_rblConsole_0">XBox 360</label>
        </td>
    </tr>
    <tr>
        <td><input id="ctl00_ContentPlaceHolder1_rblConsole_1" type="radio" name="ctl00$ContentPlaceHolder1$rblConsole" value="playstation" /><label for="ctl00_ContentPlaceHolder1_rblConsole_1">Playstation 3</label>
        </td>
    </tr>
</table>

On the javascript onclick on the checkbox I want to disable the radio buttons in the rblConsole radiobutton list. 在javascript onclick复选框上,我要禁用rblConsole单选按钮列表中的单选按钮。

I'm trying to get at the radio buttons via the jQuery endswith selector: 我正在尝试通过jQuery endswith选择器获取单选按钮:

function ToggleEnabled() {
        var isChecked = $("*[id$='chkChange']").is(":checked");
        if (isChecked) {
            $("*[name$='rblConsole'").removeAttr("disabled");
        } else {
            $("*[name$='rblConsole'").attr("disabled", "disabled");
        }
    }

So, how to disable these via jQuery? 那么,如何通过jQuery禁用这些功能?

first, remove the apostrophy in the attribute selector 首先,在属性选择器中删除撇号

function ToggleEnabled() {
        var isChecked = $("*[id$='chkChange']").is(":checked");
        if (isChecked) {
            $("*[name$=rblConsole").removeAttr("disabled");
        } else {
            $("*[name$=rblConsole").attr("disabled", "disabled");
        }
    }

second, it is better tu use the ClientID property of the controls to get the elements ids: 其次,最好使用控件的ClientID属性获取元素ID:

function ToggleEnabled() {
        var isChecked = $("#<%=chkChange.ClientID %>").is(":checked");
        if (isChecked) {
            $("#<%=rblConsole.ClientID %>").removeAttr("disabled");
        } else {
            $("#<%=rblConsole.ClientID %>").attr("disabled", "disabled");
        }
    }

I was missing the closing square bracket in the selector. 我在选择器中缺少右方括号。 It should be: 它应该是:

$("*[name$='rblConsole']").attr("disabled", "disabled");

Doh! 卫生署! My bad. 我的错。

Below code works for me, 下面的代码对我有用,

For Disable 禁用

$("table[id$=<%= rblConsole.ClientID %>] input").attr("disabled", "disabled");

For Enable 启用

$("table[id$=<%= rblConsole.ClientID %>] input").removeAttr("disabled");

请检查以下内容:

$("input[type=radio][value=EIN]").prop("disabled", true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM