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
};
You can get the value of selected radio button by Id using this in javascript/jQuery. $("#InvCopyRadio:checked"). val();
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();
var checkedradio = $('[name="gr"]:radio:checked'). val();
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With