使用 JavaScript 禁用单选按钮列表项

发布于 2025-01-08 19:16:04 字数 791 浏览 0 评论 0原文

我正在尝试使用 JavaScript 从 asp RadioButtonList 禁用单选按钮。这是我所拥有的:

<asp:RadioButton ID="rbPlanner" runat="server" onclick="deselectRadioListItem('P');" />

它调用此客户端 JavaScript onClick...

function deselectRadioListItem(radioValue) {
    var clientID = ('<%= rblSummaryOptions.ClientID %>');
    for (i = 0; i < '<%= rblSummaryOptions.Items.Count %>'; i++) {
        if (document.getElementById(clientID + "_" + i.toString()).value == radioValue) {
            (clientID + "_" + i.toString()).disabled === true;
        }
        else
        {
            (clientID + "_" + i.toString()).disabled === false;
        }
    }
}

一切似乎都工作正常(触发、迭代、if 语句工作),但是单选按钮控件并未被禁用,即使逻辑被击中。我缺少什么?非常感谢帮助!

I'm attempting to disable a radio button from an asp RadioButtonList using JavaScript. Here is what I have:

<asp:RadioButton ID="rbPlanner" runat="server" onclick="deselectRadioListItem('P');" />

which calls this client JavaScript onClick...

function deselectRadioListItem(radioValue) {
    var clientID = ('<%= rblSummaryOptions.ClientID %>');
    for (i = 0; i < '<%= rblSummaryOptions.Items.Count %>'; i++) {
        if (document.getElementById(clientID + "_" + i.toString()).value == radioValue) {
            (clientID + "_" + i.toString()).disabled === true;
        }
        else
        {
            (clientID + "_" + i.toString()).disabled === false;
        }
    }
}

Everything appears to be working correctly (fires, iterates, if-statements work) however, the radiobutton control is not becoming disabled, even though the logic is hit. What am I missing? Help is much appreciated!

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

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

发布评论

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

评论(2

终难遇 2025-01-15 19:16:04

=== 运算符用于比较。您需要使用=

function deselectRadioListItem(radioValue) {
    var clientID = ('<%= rblSummaryOptions.ClientID %>');
    for (i = 0; i < '<%= rblSummaryOptions.Items.Count %>'; i++) {
        if (document.getElementById(clientID + "_" + i.toString()).value == radioValue) {
            document.getElementById(clientID + "_" + i.toString()).disabled = true;
        }
        else
        {
            document.getElementById(clientID + "_" + i.toString()).disabled = false;
        }
    }
}

或者清理一点:

function deselectRadioListItem(radioValue) {
    var clientID = ('<%= rblSummaryOptions.ClientID %>');
    for (i = 0; i < '<%= rblSummaryOptions.Items.Count %>'; i++) {
        var objCurrentRdo = document.getElementById(clientID + "_" + i.toString());

        if (objCurrentRdo.value == radioValue)
            objCurrentRdo.disabled = true;
        else
            objCurrentRdo.disabled = false;
    }
}​

其他信息:查看 MDN 上的比较运算符

The === operator is used for comparison. You need to use =.

function deselectRadioListItem(radioValue) {
    var clientID = ('<%= rblSummaryOptions.ClientID %>');
    for (i = 0; i < '<%= rblSummaryOptions.Items.Count %>'; i++) {
        if (document.getElementById(clientID + "_" + i.toString()).value == radioValue) {
            document.getElementById(clientID + "_" + i.toString()).disabled = true;
        }
        else
        {
            document.getElementById(clientID + "_" + i.toString()).disabled = false;
        }
    }
}

Or cleaned up a tiny bit:

function deselectRadioListItem(radioValue) {
    var clientID = ('<%= rblSummaryOptions.ClientID %>');
    for (i = 0; i < '<%= rblSummaryOptions.Items.Count %>'; i++) {
        var objCurrentRdo = document.getElementById(clientID + "_" + i.toString());

        if (objCurrentRdo.value == radioValue)
            objCurrentRdo.disabled = true;
        else
            objCurrentRdo.disabled = false;
    }
}​

Additional Information: Check out Comparison Operators on MDN.

三生一梦 2025-01-15 19:16:04

这肯定是错误的。

(clientID + "_" + i.toString()).disabled === true;

也许你不会打字

document.getElementById(clientID + "_" + i.toString()).disabled = true;

,或者更好的

function deselectRadioListItem(radioValue) {
        var clientID = ('<%= rblSummaryOptions.ClientID %>');
        for (i = 0; i < <%= rblSummaryOptions.Items.Count %>; i++) {
                document.getElementById(clientID + "_" + i).disabled = 
              document.getElementById(clientID + "_" + i).value == radioValue;
        }
    }

是你这里有一些错误......

this is for sure wrong.

(clientID + "_" + i.toString()).disabled === true;

maybe you won to type

document.getElementById(clientID + "_" + i.toString()).disabled = true;

or even better

function deselectRadioListItem(radioValue) {
        var clientID = ('<%= rblSummaryOptions.ClientID %>');
        for (i = 0; i < <%= rblSummaryOptions.Items.Count %>; i++) {
                document.getElementById(clientID + "_" + i).disabled = 
              document.getElementById(clientID + "_" + i).value == radioValue;
        }
    }

you have some bugs here...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文