2011-12-07 87 views
2

我有一个RadioButtonList控件在我的网页上有多个选项。我想要处理其选项的更改,并根据所选选项的值,工作。如何处理单选按钮列表选项更改与jQuery

这不起作用:

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

我如何处理这个问题?

感谢

回答

16

你只需要绑定的投入本身,而不是他们的小组:

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

他说,那他有多个复选框。使用'id'对于这个解决方案并不好。 – Oyeme

+0

我相信他的身份证是由ASP生成的一组div,围绕一组元素。 – Interrobang

1

可能是这样一个帮你:)

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

     }); 
0
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> 
+0

已经回答了。虽然它很有用,但这似乎是对问题和答案的改写。 – vijayst