如何通过 jQuery 禁用 ASP.NET 单选按钮列表

发布于 2024-07-17 17:23:45 字数 1548 浏览 14 评论 0原文

我有一个复选框和单选按钮列表,定义如下:

<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>

这些控件位于带有母版页的内容页面中,因此呈现的实际 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>

在复选框上的 javascript onclick 上,我想禁用 rblConsole 单选按钮列表中的单选按钮。

我正在尝试通过 jQueryendswith 选择器访问单选按钮:

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

那么,如何通过 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:

<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.

I'm trying to get at the radio buttons via the jQuery endswith selector:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不如归去 2024-07-24 17:23:45

首先,删除属性选择器中的撇号

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

其次,最好使用控件的 ClientID 属性来获取元素 id:

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

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:

function ToggleEnabled() {
        var isChecked = $("#<%=chkChange.ClientID %>").is(":checked");
        if (isChecked) {
            $("#<%=rblConsole.ClientID %>").removeAttr("disabled");
        } else {
            $("#<%=rblConsole.ClientID %>").attr("disabled", "disabled");
        }
    }
寄居人 2024-07-24 17:23:45

我错过了选择器中的右方括号。 应该是:

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

呵呵! 我的错。

I was missing the closing square bracket in the selector. It should be:

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

Doh! My bad.

对岸观火 2024-07-24 17:23:45

下面的代码对我有用,

对于禁用

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

对于启用

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

Below code works for me,

For Disable

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

For Enable

$("table[id$=<%= rblConsole.ClientID %>] input").removeAttr("disabled");
ぶ宁プ宁ぶ 2024-07-24 17:23:45

请检查一下:

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

Please check this:

$("input[type=radio][value=EIN]").prop("disabled", true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文