admin管理员组

文章数量:1330167

 <asp:RadioButtonList ID="rbl" runat="server" onMouseUp="Myf()">
    <asp:ListItem Text="Yes" Value="1"></asp:ListItem>
    <asp:ListItem Text="No" Value="0"></asp:ListItem>        
    </asp:RadioButtonList>




function Myf() {
            var list = document.getElementById("<%=rbl.ClientID %>"); 
           var inputs = list.getElementsByTagName("input");
            var selected;
            for (var i = 0; i < inputs.length; i++) {
                alert(inputs[i].innerHTML);

            }        

        }

I'm getting the value of Radiobuttonlist. How to get the Text of the Radiobuttonlist?

 <asp:RadioButtonList ID="rbl" runat="server" onMouseUp="Myf()">
    <asp:ListItem Text="Yes" Value="1"></asp:ListItem>
    <asp:ListItem Text="No" Value="0"></asp:ListItem>        
    </asp:RadioButtonList>




function Myf() {
            var list = document.getElementById("<%=rbl.ClientID %>"); 
           var inputs = list.getElementsByTagName("input");
            var selected;
            for (var i = 0; i < inputs.length; i++) {
                alert(inputs[i].innerHTML);

            }        

        }

I'm getting the value of Radiobuttonlist. How to get the Text of the Radiobuttonlist?

Share Improve this question edited Jun 28, 2013 at 12:13 Pearl asked Jun 28, 2013 at 12:06 PearlPearl 9,4458 gold badges45 silver badges60 bronze badges 2
  • 1 Can you provide this code in Jsfiddle – S. S. Rawat Commented Jun 28, 2013 at 12:09
  • Radiobuttons do not have text. Text is in label element. – YD1m Commented Jun 28, 2013 at 12:11
Add a ment  | 

5 Answers 5

Reset to default 6

First of all the radio buttons do not have text in your code

Second, if you look at the source of the resulting html page, you can see that the radio buttons have labels that store the text. So you can look for label instead of input in your function and it will get the text

    function Myf() {
        var list = document.getElementById("<%=rbl.ClientID %>");
        var inputs = list.getElementsByTagName("label");
        var selected;
        for (var i = 0; i < inputs.length; i++) {
            alert(inputs[i].innerHTML);

        }
    }

Use the getAttribute() method.

element.getAttribute("Text");

More info on: https://developer.mozilla/en-US/docs/Web/API/element.getAttribute?redirectlocale=en-US&redirectslug=DOM%2Felement.getAttribute

To get text you have to put "label" instead of "input" in getelement.

try like this:

    var inputs = list.getElementsByTagName("label");
<asp:RadioButtonList ID="rbl" runat="server">
    <asp:ListItem Text="Yes" Value="1"></asp:ListItem>
    <asp:ListItem Text="No" Value="0"></asp:ListItem>        
    </asp:RadioButtonList>

$(document).ready(function () {
    $('[id*="rdl"],[type="radio"]').change(function () {
        if ($(this).is(':checked'))
            console.log($(this).next('label').html());
    });
});

try this

    function Myf() {
        $("#<%=rbl.ClientID %>").find('input').each(function () {
            var this_input = $(this);
            alert($("#<%=rbl.ClientID %>").find('label[for="' + this_input.attr('id') + '"]').text());
        })

    }

本文标签: cHow to get the Text of the RadiobuttonlistStack Overflow