Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading the selected value from asp:RadioButtonList using jQuery

I've got code similar to the following...

<p><label>Do you have buffet facilities?</label>   <asp:RadioButtonList ID="blnBuffetMealFacilities:chk" runat="server">     <asp:ListItem Text="Yes" Value="1"></asp:ListItem>     <asp:ListItem Text="No" Value="0"></asp:ListItem>   </asp:RadioButtonList></p> <div id="HasBuffet">   <p><label>What is the capacity for the buffet?</label>   <asp:RadioButtonList ID="radBuffetCapacity" runat="server">     <asp:ListItem Text="Suitable for upto 30 guests" value="0 to 30"></asp:ListItem>     <asp:ListItem Text="Suitable for upto 50 guests" value="30 to 50"></asp:ListItem>     <asp:ListItem Text="Suitable for upto 75 guests" value="50 to 75"></asp:ListItem>     <asp:ListItem Text="Suitable for upto 100 guests" value="75 to 100"></asp:ListItem>     <asp:ListItem Text="Suitable for upto 150 guests" value="100 to 150"></asp:ListItem>     <asp:ListItem Text="Suitable for upto 250 guests" value="150 to 250"></asp:ListItem>     <asp:ListItem Text="Suitable for upto 400 guests" value="250 to 400"></asp:ListItem>   </asp:RadioButtonList></p> </div> 

I want to capture an event when the radio list blBuffetMealFacilities:chk changes client side and perform a slide down function on the HasBuffet div from jQuery. What's the best way to create this, bearing in mind there are several similar sections on the page, where I want questions to be revealed depending on a yes no answer in a radio list.

like image 943
digiguru Avatar asked Nov 21 '08 09:11

digiguru


People also ask

How to set RadioButtonList selected value in jQuery?

$('#<%=rbAttachmentType. ClientID %>'). find("input[value='0']"). attr("checked", "checked");

How to set checked radio button using jQuery?

$(function() { var $radios = $('input:radio[name=gender]'); if($radios.is(':checked') === false) { $radios. filter('[value=Male]'). prop('checked', true); } }); Hope it works!

How to bind value to radio button in jQuery?

The most straightforward option, this is. $("#radioID") // select the radio by its id . change(function(){ // bind a function to the change event if( $(this).is(":checked") ){ // check if the radio is checked var val = $(this). val(); // retrieve the value } });


2 Answers

The simple way to retrieve checked value of RadioButtonList1 is:

$('#RadioButtonList1 input:checked').val() 

Edit by Tim:

where RadioButtonList1 must be the ClientID of the RadioButtonList

var rblSelectedValue = $("#<%= RadioButtonList1.ClientID %> input:checked");  
like image 180
Vinh Avatar answered Oct 10 '22 13:10

Vinh


this:

$('#rblDiv input').click(function(){     alert($('#rblDiv input').index(this)); }); 

will get you the index of the radio button that was clicked (i think, untested) (note you've had to wrap your RBL in #rblDiv

you could then use that to display the corresponding div like this:

$('.divCollection div:eq(' + $('#rblDiv input').index(this) +')').show(); 

Is that what you meant?

Edit: Another approach would be to give the rbl a class name, then go:

$('.rblClass').val(); 
like image 36
Andrew Bullock Avatar answered Oct 10 '22 12:10

Andrew Bullock