【问题标题】:How to Handle Radio Button List Option change with jQuery如何使用 jQuery 处理单选按钮列表选项更改
【发布时间】:2016-07-27 21:05:41
【问题描述】:

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

这不起作用:

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

我该如何处理?

谢谢

【问题讨论】:

    标签: javascript jquery asp.net radio-button


    【解决方案1】:

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

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

    【讨论】:

    • 他说,他有多个复选框。使用“id”不适合这个解决方案。
    • 我相信他的 ID 是由 ASP 作为围绕一组 元素的 div 生成的。
    【解决方案2】:

    也许这个对你有帮助:)

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

    【讨论】:

      【解决方案3】:
      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>
      

      【讨论】:

      • 已经回答了。虽然它很有用,但这似乎是对问题和答案的重新表述。
      猜你喜欢
      • 2010-12-04
      • 1970-01-01
      • 2021-07-09
      • 1970-01-01
      • 1970-01-01
      • 2012-04-12
      • 2012-10-20
      • 2010-10-31
      • 2016-07-19
      相关资源
      最近更新 更多