Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i get selected value from html.radiobuttonfor in jquery

I have 2 radio buttons in MVC bound to a model as shown below. How do i get the selected radio button value in jquery?

<label>Yes</label>
@Html.RadioButtonFor(m => m.SelectedPerson.is_selected, true, new { @onchange = "ToggleInfo();" })
<label>No</label>
 @Html.RadioButtonFor(m => m.SelectedPerson.is_selected, false, new {  @onchange = "ToggleInfo();" })

The javascript funtion being called is:

function ToggleInfo() {
    //Get the selected value here to tell if Yes/True or No/False is selected       
};
like image 687
Ben Avatar asked Oct 20 '15 15:10

Ben


People also ask

How to get value of selected radio button using jQuery?

You can get the value of selected radio button by Id using this in javascript/jQuery. $("#InvCopyRadio:checked"). val();

How to get dynamic radio button value in jQuery?

function updateAmount() { var selectedValue = ""; var selected = $("input:radio[name='qualification']:checked"); if (selected. length > 0) { selectedValue = selected. val(); alert('The radio button has been selected and the value is. ' + selectedValue); } } updateAmount();

How to get selected value of radio button in controller?

var checkedradio = $('[name="gr"]:radio:checked'). val();


1 Answers

You have two ways, the first is

function ToggleInfo() {
    var result = $('[name="SelectedPerson.is_selected"]:checked').val();
};

The second is

<label>Yes</label>
@Html.RadioButtonFor(m => m.SelectedPerson.is_selected, true, new { @onchange = "ToggleInfo(this);" })
<label>No</label>
 @Html.RadioButtonFor(m => m.SelectedPerson.is_selected, false, new {  @onchange = "ToggleInfo(this);" })

function ToggleInfo(el) {
     var result = el.value;   
 }

Here a fiddle for you to check it https://dotnetfiddle.net/FcVRHA

like image 139
DevT Avatar answered Oct 31 '22 17:10

DevT