使用 javascript 启用/禁用单选按钮列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8072924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 02:09:57  来源:igfitidea点击:

enable/disable radiobuttonlist with javascript

javascriptasp.net

提问by Laziale

I am trying to enable/disable radiobuttonlist with javascript code. This javascript is working fine with textboxes but it looks like that it doesn't work with radiobuttonlist. Here is the code I am using:

我正在尝试使用 javascript 代码启用/禁用单选按钮列表。这个 javascript 对文本框工作正常,但看起来它不适用于 radiobuttonlist。这是我正在使用的代码:

 var chkEPM = document.getElementById("<%=chkEPM.ClientID %>");
        chkEPM.onchange = function () {
            if (this.checked == true)
                document.getElementById("<%=rblEPM.ClientID %>").disabled = false;
            else
                document.getElementById("<%=rblEPM.ClientID %>").disabled = true;
        };

Thanks in advance for each reply and have a good day/night

提前感谢您的每一个回复,祝您早日/晚安

回答by kevev22

You can try this:

你可以试试这个:

function changeItemState(disable)
{
    rb = document.getElementById("<%=rblEPM.ClientID %>");

    var rbItems = rb.getElementsByTagName('input');

    for (var itemIndex = 0; itemIndex < rbItems.length; itemIndex++) 
    {
        rbItems[itemIndex].disabled = disable;
    }
}

var chkEPM = document.getElementById("<%=chkEPM.ClientID %>");
chkEPM.onchange = function () {
    if (this.checked == true)
        changeItemState(false);
    else
        changeItemState(true);
};

Or if you can use jquery 1.6 or greater you could do:

或者,如果您可以使用 jquery 1.6 或更高版本,则可以执行以下操作:

$("#<%=rblEPM.ClientID %>").find('input').prop('disabled', 'true');

回答by Nick Rolando

Try something like

尝试类似

document.getElementById('rbl').setAttribute('disabled', 'disabled');

rblbeing the ID of your radiobuttonlist.

rbl是您的单选按钮列表的 ID。

回答by Suraj

 function EnableDisableRadio(CheckBox1) {
            var controls = document.getElementById("<%=Panel1.ClientID%>").getElementsByTagName("input");
            for (var i = 0; i < controls.length; i++)
                controls[i].disabled = CheckBox1.checked ? false : true;
        }

回答by Unkown

For cross platform support u need to use something like this one:

对于跨平台支持,您需要使用这样的东西:

$('#RadioButtonList1').find('*').each(function () 
{ 
    $(this).attr("disabled", true); 
});