javascript 单选按钮在javascript中列出选定的项目值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15269311/
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-27 00:07:10  来源:igfitidea点击:

Radio Button list selected items value in javascript

javascriptasp.net

提问by Pramod

I am using following code to get the selected elements value in radio button list.

我正在使用以下代码获取单选按钮列表中选定的元素值。

function SelectRadioButton()
{
   var radiobutton = document.getElementsByName('<%=RadioButtonList1.ClientID %>');
   alert(radiobutton.length);
for(var x = 0; x < radiobutton.length; x++)
            {
                if(radiobutton[x].checked)
                {
                    alert('selected is ' + radiobutton[x].id);
                }
             }
 }

Following is the HTML markup

以下是 HTML 标记

  <table id="ctl00_ContentPlaceHolder1_idControl_RadioButtonList1" class="chk" onclick="javascript:SelectRadioButton(this, ctl00_ContentPlaceHolder1_idControl_RadioButtonList1)" border="0">
    <tr>
        <td><input id="ctl00_ContentPlaceHolder1_idControl_RadioButtonList1_0" type="radio" name="ctl00$ContentPlaceHolder1$idControl$RadioButtonList1" value="1" checked="checked" /><label for="ctl00_ContentPlaceHolder1_idControl_RadioButtonList1_0">List</label></td><td><input id="ctl00_ContentPlaceHolder1_idControl_RadioButtonList1_1" type="radio" name="ctl00$ContentPlaceHolder1$idControl$RadioButtonList1" value="2" /><label for="ctl00_ContentPlaceHolder1_idControl_RadioButtonList1_1">Assignment</label>

But I am getting length 0 in alert(radiobutton.length); statement. Why is this happening. any thing that I am missing?

但是我在 alert(radiobutton.length); 中得到了长度 0; 陈述。为什么会发生这种情况。有什么我想念的吗?

采纳答案by Sachin

You can use jquery to do this.

您可以使用 jquery 来执行此操作。

 alert($(".chk").find("input:checked").length); // chk is your css class name applied to Checkbox List element.

You can get specific element by using this

您可以使用此获取特定元素

alert($(".chk").find("input:checked")[0]);

回答by ShadowScripter

Here's how you do it with javascript only, if you don't want to use getElementById

如果您不想使用,以下是仅使用 javascript 的方法 getElementById



Code | JSFiddle

代码 | JSFiddle

function SelectRadioButton(){
    var radiolist = getElementsByClass("table", "chk")[0],
        radios = radiolist.getElementsByTagName("input");

    for(var i = 0; i < radios.length; i++){
        if(radios[i].checked){
            alert('Selected radiobutton is ' + radios[i].id);
        }
    }
 }

function getElementsByClass(tag, name){
    var elements = document.getElementsByTagName(tag);
    var ret = [];

    for(var i = 0; i < elements.length; i++){
        if(elements[i].className.indexOf(name) !== -1){
            ret.push(elements[i]);
        }
    }

    return ret;
}

回答by Adil

RadioButtonList1will be converted to radio buttons with ids having RadioButtonList1, You can iterate through DOM and look for matched idsand put them in some array or directly perform what you want to them.

RadioButtonList1将转换为带有 id 的单选按钮RadioButtonList1,您可以遍历 DOM 并查找匹配项ids并将它们放入某个数组中,或者直接对它们执行您想要的操作。

radiobutton = [];

for(i=0;i<document.forms[0].length;i++)
{
    e=document.forms[0].elements[i];
    if (e.id.indexOf("RadioButtonList1") != -1 )
    {
           radiobutton.push(e); 

    }       
}