Javascript 通过javascript从radiobuttonlist中获取值?

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

Get the value from radiobuttonlist through javascript?

javascriptasp.net

提问by vikas

I have a radibutton list.

我有一个单选按钮列表。

          <asp:RadioButtonList ID="RadioButtonList1" runat="server" 
              RepeatDirection="Horizontal" RepeatLayout="flow" >
             <asp:ListItem Selected="True" Text ="Yes" Value="0"></asp:ListItem>
             <asp:ListItem  Text ="No" Value="1"></asp:ListItem>
          </asp:RadioButtonList>

And to get the value from radio list I have a function :

为了从广播列表中获取值,我有一个功能:

 function getvalueofradiolist() {

        var radiolist = document.getElementById('<%= RadioButtonList1.ClientID %>');
        var rblName = radiolist.name;
        var radio = document.getElementsByName(rblName);

        for (var x = 0; x < radio.length; x++) {
            if (radio[x].checked) {
                alert("Selected item Value " + radio[x].value);
            }
        }
    }

But it returns undefined.

但它返回未定义。

Did I write something wrong?

我写错了吗?

I am calling this function on button click.

我在单击按钮时调用此函数。

Suggest me If I can do this with the help of code behind in spite of Javascript.

建议我如果我可以在背后的代码的帮助下做到这一点,尽管有 Javascript。

回答by Afshin

<form runat="server">
<asp:RadioButtonList ID="RadioButtonList1" runat="server" 
          RepeatDirection="Horizontal" RepeatLayout="flow" >
         <asp:ListItem Selected="True" Text ="Yes" Value="0"></asp:ListItem>
         <asp:ListItem  Text ="No" Value="1"></asp:ListItem>
      </asp:RadioButtonList>    </form>
<p id="button" onclick="getvalue()">click me</p>

<script type="text/javascript">
function getvalue(){
 alert($('#<%=RadioButtonList1.ClientID %> input[type=radio]:checked').val());
}
</script>

回答by Tom

regarding the code

关于代码

var rblName = radiolist.name;

you don't even have name attribute

你甚至没有 name 属性

回答by Sandeep Pathak

You could try :

你可以试试:

function getvalueofradiolist() {

            var radioButtonlist = document.getElementsByName("<%=RadioButtonList1.ClientID%>");
            for (var x = 0; x < radioButtonlist.length; x++) {
                if (radioButtonlist[x].checked) {
                    alert("Selected item Value "  + radioButtonlist[x].value);
                }
}

回答by Samuel Caillerie

An dwhat about getting the inputelements inside directly :

关于input直接获取元素的方法:

function getvalueofradiolist() {
    var radiolist = document.getElementById('<%= RadioButtonList1.ClientID %>');
    var radio = radiolist.getElementsByTagName("input");

    for (var x = 0; x < radio.length; x++) {
        if (radio[x].type === "radio" && radio[x].checked) {
            alert("Selected item Value " + radio[x].value);
        }
    }
}