lundi 3 mars 2014

How to use checkall when chekced one checkbox in jquery


Vote count:

0




I have table where it has five checkboxes(Add,Edit,View,Delete,All). This table rows are generated dynamically. Code for that is as follows:



<div class="tablecontatiner">

<table>
<tr>
<th>Function</th>
<th>Add</th>
<th>Edit</th>
<th>View</th>
<th>Delete</th>
<th>All</th>
</tr>
@foreach (var item in Model)
{
<tr class="grouprow">
<td><input type="hidden"/><input id="@(item.Func_Code)" type="checkbox" style="visibility:hidden" />@item.FunctionName</td>
<td><input id="@(item.Func_Code + "A")" type="checkbox" value="Add" @(item.Add==true?"checked":"") /></td>
<td><input id="@(item.Func_Code + "E")" type="checkbox" value="Edit" @(item.Edit==true?"checked":"") /></td>
<td><input id="@(item.Func_Code + "V")" type="checkbox" value="View" @(item.View==true?"checked":"")/></td>
<td><input id="@(item.Func_Code + "D")" type="checkbox" value="Delete" @(item.Delete==true?"checked":"")/></td>
<td><input id="@(item.Func_Code + "ALL")" type="checkbox" value="All"/></td>
</tr>
}
</table>
</div>


i am trying to work around my last checkbox(All).My requirement is when i check that "All" checkbox,remaining checkboxes should be checked and when i uncheck "All" checkbox,remaining checkboxes should be unchecked.


I am using following Jquery for this.



<script>
$('.CheckAll').on('change', function () {
$(this).closest('.grouprow').find(':checkbox').not(this).prop('checked', this.checked);
});
$('table tr input:checkbox:not(".CheckAll")').on('change', function () {
if (!this.checked) {
$('.CheckAll').prop('checked', false);
}

//var checkedBoxes = $(this).closest('.grouprow').find('input:checkbox:not(".CheckAll"):checked');
//if (checkedBoxes.length == 4) {
// $('.CheckAll').prop('checked', true);
//}
});
</script>


everything works fine,but when i uncheck any one of the checkbox in any row when all the remaining rows checkboxes are checked,the "All" checkbox for all the rows is unchecked. Any help?



asked 1 min ago






Aucun commentaire:

Enregistrer un commentaire