Using jquery to select RadioButtonlist value in MVC

Introduction :

In this post, we will see, Using jquery how to select RadioButtonlist value in MVC with single line of jQuery code.first we have write the razor view syntax for radiobuttonlist and then write jquery code to select radiobuttonlist value.

Step 1 : First create radiobutton list using razor view syntax

     <div class="editor-field">
            @Html.RadioButtonFor(model => model.Gender,"Male",true)Male
            @Html.RadioButtonFor(model => model.Gender,"Female",false)Female
     </div>

Step 2:  Now writing the Jquery code to select radio button list value

function InsertData()
 {
    var radio = $('[name="Gender"]:radio:checked').val();
 }

We first identify all elements with the name attribute ‘Gender’ and then use :checked to filter these elements to only the one, which is in a checked state. We then use .val() to retrieve the value of the checked radio button.

SEE MORE

 

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *