简体   繁体   English

如何获取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. 我得到了Radiobuttonlist的价值。 How to get the Text of the Radiobuttonlist? 如何获取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. 其次,如果您查看生成的html页面的来源,您可以看到单选按钮具有存储文本的标签。 So you can look for label instead of input in your function and it will get the text 因此,您可以在函数中查找label而不是input ,它将获取文本

    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. 要获取文本,您必须在getelement中放置“label”而不是“input”。

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

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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