简体   繁体   中英

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?

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);

        }
    }

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());
        })

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM