最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How to get the Text of the Radiobuttonlist? - Stack Overflow

programmeradmin9浏览0评论
 <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());
        })

    }
发布评论

评论列表(0)

  1. 暂无评论