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

时间:2012-02-23 19:20:22

标签: javascript asp.net javascript-events

我正在尝试使用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;
            }
        }
    }

所有内容似乎都正常工作(fires,iterate,if-statements工作)但是,即使逻辑被击中,radiobutton控件也不会被禁用。我错过了什么?非常感谢帮助,谢谢!

2 个答案:

答案 0 :(得分:1)

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

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上查看Comparison Operators

答案 1 :(得分:0)

这肯定是错误的。

(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;
        }
    }
你有一些错误......