zSynopsis
zSynopsis

Reputation: 4854

Jquery get Selected Radio Button from List on page load

I was wondering if anyone can post an example of how to get a selected radio button option from an asp.net radio button list control via jquery on the loading of a page.

Thanks

Upvotes: 5

Views: 13056

Answers (3)

Vinod kumar kushawaha
Vinod kumar kushawaha

Reputation: 11

protected void radioButton_CheckedChanged(object sender, EventArgs e)
{
  throw new ApplicationException("Radio Changed");
  RadioButton rb = (RadioButton)sender;
  TextBox tbexact = (TextBox)this.UpdatePanel1.FindControl("TextBoxExact");
  TextBox tbpartial = (TextBox)this.UpdatePanel1.FindControl("TextBoxPartial");
  DropDownList dropdown = (DropDownList)this.UpdatePanel1.FindControl("DropDownListCountries");

  RadioButton rbc = (RadioButton)this.UpdatePanel1.FindControl("RadioButtonExact");
  if (tbexact == null)
    throw new ApplicationException("Could not find control");
  else
    throw new ApplicationException("Found it");
  if (rbc != null && rb.Equals(rbc))
  {
    tbpartial.Enabled = false;
    dropdown.Enabled = false;
    mCriteria = SearchCriteria.Exact;
  }
  rbc = (RadioButton)this.UpdatePanel1.FindControl("RadioButtonPartial");
  if (rbc != null && rb.Equals(rbc))
  {
    tbexact.Enabled = false;
    dropdown.Enabled = false;
    mCriteria = SearchCriteria.Partial;
  }
  rbc = (RadioButton)this.UpdatePanel1.FindControl("RadioButtonPerCountry");
  if (rbc != null && rb.Equals(rbc))
  {
    tbexact.Enabled = false;
    tbpartial.Enabled = false;
    mCriteria = SearchCriteria.Country;
  }
}

Upvotes: 1

Dan Davies Brackett
Dan Davies Brackett

Reputation: 10081

Working example here.

The selector I used to get the radio buttons will grab all the radio buttons with the class ofinterest on the page.

$(function(){ 
  var value = $('input.ofinterest:checked').val();
  $('#result').text(value); 
});

If you want to scope the selector further, and you don't mind writing your JS directly in your aspx/ascx, you can use Scott's solution above instead. But if you give the buttons you're interested in a known classname, you can put this JS in a .js file.

Upvotes: 1

Scott Ivey
Scott Ivey

Reputation: 41588

In your javascript function where you want to query the list, use this code..

var selected = jQuery('#<%= MyRadioButtonList.ClientID %> input:checked').val();
// or ...
var selected = $('#<%= MyRadioButtonList.ClientID %> input:checked').val();

to set a sample label with the results of your selected radiobuttonlist, you could do this...

$(document).ready(function(){
    var selected = $('#<%= MyRadioButtonList.ClientID %> input:checked').val();
    $("#<%= MySampleLabel.ClientID %>").text(selected);
}

Upvotes: 6

Related Questions