lundi 16 février 2015

checkbox that checks only one row in a checkboxes table jQuery

It seems that it's not a big deal but i still can't firgure out the problem: In my razor view I display many checkboxes organized in three columns, and I want to create a checkbox for every row that checks all the row (every 3 checkboxes), so here is what i did using jQuery:



<table>
<tr>
@{
int cnt = 0;
string sr =null;
string j = null;
List<Security.Models.AssignedRolesVM> Roles = ViewBag.tp_role;

foreach (var role in Roles)
{
if (cnt++ % 3 == 0)
{ sr=cnt.ToString();
j =(cnt+1).ToString();
@:</tr><tr>
@: <td><input type="checkbox" id=j /></td>
<script src="~/Scripts/jquery-1.10.2.min.js">
</script>
<script type="text/javascript">
$(function () {
$(j).click(function () {
$('input[name=sr]').prop("checked", $(this).prop("checked"));
});
$('input[name=sr]').click(function () {
if ($('input[name=sr]').length == $('input[name=sr]:checked').length) {
$(j).prop("checked", "checked");
}
else {
$(j).removeProp("checked");
}

});
});
</script>
}
@:<td>
<input type="checkbox"
name=sr
class="checked"
value="@role.RoleID"
@(Html.Raw(role.Assigned ? "checked=\"checked\"" : "")) />
@role.RoleID @: @role.Name
@:</td>
}
@:</tr>
}
</table>


but instead of checking only the row, it does check all the checkboxes. It seems to me that the loop is correct (sr should change every time it is a modulo 3) , any help?





Aucun commentaire:

Enregistrer un commentaire