Using jquery to select Checkboxlist value in MVC

Introduction:

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

Step 1 : First create Checkboxlist list using razor view syntax

   
    @foreach (var item in @ViewBag.Hobby)
    {      
       <input type="checkbox" name="ips"  value="@item.HobbyName" />@item.HobbyName
    }

Step 2:  Now writing the Jquery code to select Checkboxlist value

   function InsertData()
   {
       var v='';
       $('input:checkbox:checked').each(function () {   
            v = v + $(this).attr('value') +",";
        });
   }

We first identify all elements with the name attribute ‘ips’ and then use :checked to filter these elements using jquery each loop, which is in a checked state. We then use .val() to retrieve the value of the checked checkboxlist.

SEE MORE

1 Comment

Leave a Reply

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