Javascript 如何使用 jQuery 处理单选按钮列表选项更改

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

How to Handle Radio Button List Option change with jQuery

javascriptjqueryasp.netradio-button

提问by Arian

I hava a RadioButtonList control on my page that has multiple option.I want to handle change of its options and, according to value of selected option, do work.

我的页面上有一个 RadioButtonList 控件,它有多个选项。我想处理其选项的更改,并根据所选选项的值进行工作。

This does not work:

这不起作用:

 $(document).ready(function () {
        $('#RadioButtonList1').change(function () {
            if ($(this).is(':checked')) {
                alert("yes");
            }
        });
    });

How I can Handle that?

我该如何处理?

thanks

谢谢

回答by Interrobang

You just need to bind the inputs themselves, not their group:

您只需要绑定输入本身,而不是它们的组:

$(document).ready(function () {
    $('#RadioButtonList1 input').change(function () {
        // The one that fires the event is always the
        // checked one; you don't need to test for this
        alert($(this).val());
    });
});

回答by Kalpesh Desai

May be this one help you:)

可能这对你有帮助:)

$('form#package_information_id').on('change','.shipment_type_radio_class',function(){

             var currentselval = $(this).find('input[type="radio"]:checked');
             //console.log(currentselval);
             if(currentselval.val()=='dropoff') 
             {
                $('.pickup_detail_class').hide();
             }else
             {
                $('.pickup_detail_class').show();
             }

        });

回答by jsuresh

enter code here

<div>
    <asp:RadioButtonList ID="rblTest" runat="server">
        <asp:ListItem>10</asp:ListItem>
        <asp:ListItem>20</asp:ListItem>
        <asp:ListItem>30</asp:ListItem>
        <asp:ListItem>40</asp:ListItem>``
    </asp:RadioButtonList>
</div>

  <script type="text/javascript">

    $(function() {

            var val = "";
            val = $('[id*=rblTest]').find('input:checked').val();
            if (val != "" && val != undefined) {
                alert("Selected item value is : " + val)
            }

        });


</script>