I have the following form where the user can mark all the clients at once:
WhenIclickSelectAll,itworkscorrectly,butwhenIuncheckoneoftheoptions,theSelectAllcheckboxisstillchecked,andwhenIuncheckSelectAllandIselectitagain,thecheckboxthatIpreviouslyuncheckeddoesnotuncheck,itisselected.
Followthecodebelow.It'sinsideamethodinPHP:
...$listar="<div class=\"rkmd-checkbox checkbox-rotate checkbox-ripple\">
<label class=\"input-checkbox checkbox-primary\">
<input type=\"checkbox\" id=\"marcarTodos\" onclick=\"marcarDesmarcar();\">
<span class=\"checkbox\" style='font-weight: bold'></span>
</label>
<div class=\"captions\" style='font-weight: bold'>SELECIONAR TODOS</div>
</div>";
$listar .= "<table class='table table-bordered'>
<thead>
<tr>
<th style='background-color: #4682B4; color: #FFF; text-align: center'>Cliente</th>
<th style='background-color: #4682B4; color: #FFF; text-align: center'>Compras</th>
</thead>
<tbody>";
$c = 1;
while($jmListar = mysqli_fetch_object($sqlListar)){
$listar .= "<tr>";
$listar .= "<td><input type='hidden' name='Clientes[]' value='".$jmListar->IdCadastros."'><i class=\"fa fa-caret-right\" aria-hidden=\"true\"></i> ".$jmListar->NomeUsuarios."</td>";
$listar .= "<td>
<div align='center'>
<div class=\"rkmd-checkbox checkbox-rotate checkbox-ripple\">
<label class=\"input-checkbox checkbox-success\">
<input type=\"checkbox\" class='marcar' name='Comprou[]' value='S' id=\"checkbox\">
<span class=\"checkbox\"></span>
</label>
<div class=\"captions\">Sim</div>
</div>
</td>";
$listar .= "</tr>";
$c++;
}
$listar .= "</table>";
$listar .= "</div>";
}
....
JQuery
function marcarDesmarcar()
{
if($("#marcarTodos").prop("checked"))
{
$(":checkbox[name='Comprou']").attr("checked","checked");
} else {
$(":checkbox[name='Comprou']").removeAttr("checked");
}
}
At last, a mess only. How do I click Select All , it marks all the checkboxes, but when you uncheck one of them, the Select All checkbox is unchecked too?