简体   繁体   中英

check all checkboxes doesn't work , asp.Net MVC

I have a view like below

<input type="checkbox" id="checkall"  name="check_all"/><span>Check All</span>
 <table class="table">
    <tr>
        <th>Selected</th>           
        <th>Name</th>         
   </tr>
    @foreach (School.Data.ViewModels.Student item in Model.StudentDetails)
    {
        <tr>
            <th>@Html.CheckBoxFor(modelitem => item.IsChecked, new { @onclick = "studentChecked(this);", @name="checked1", @class="idrow" })  </th>  
             <th> @Html.DisplayFor(modelitem => item.Name)</th>      
        </tr>
    }

 </table>

 <script>
    $(document).on(' change', 'input[name="check_all"]', function () {
       $('.idRow').prop("checked1", this.checked);
    });
 </script>

I have one checkbox by the name "check_all". When I select this checkbox all checkboxes in the table must be selected. How can I do this?. I have seen this page http://jsfiddle.net/GW64e/ , but still didn't work. please help :)

Below code will check all the checkbox

  $("#checkall").click(function () {
        $(".idrow").attr('checked', this.checked);
    });

Below code will uncheck and check 'check all' checkbox based on selection

$(".idrow").click(function() {
    if (!this.checked) {
        $("#checkall").attr('checked', false);
    }
    else if ($(".idrow").length == $(".idrow:checked").length) {
        $("#checkall").attr('checked', true);
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM