Colleagues.
I have a system to register the student's absence, it was done individually, that is, I selected a particular student and used that toogle:
Functionalcode
<divclass="form-group">
<label for="NomeEscola">O aluno faltou?</label>
<div class="input-group">
<div id="radioBtn" class="btn-group">
<a class="btn btn-primary btn-sm active" data-toggle="presenca" data-title="N" style="border: 1px solid #000">Não</a>
<a class="btn btn-danger btn-sm notActive" data-toggle="presenca" data-title="S" style="border: 1px solid #000">Sim</a>
</div>
<input type="hidden" name="Faltou" id="presenca">
</div>
</div>
<script type="text/javascript">
$('#radioBtn a').on('click', function(){
var sel = $(this).data('title');
var tog = $(this).data('toggle');
$('#'+tog).prop('value', sel);
$('a[data-toggle="'+tog+'"]').not('[data-title="'+sel+'"]').removeClass('active').addClass('notActive');
$('a[data-toggle="'+tog+'"][data-title="'+sel+'"]').removeClass('notActive').addClass('active');
})
</script>
Individually it works, but the structure has changed. The school now selects a class and listing the students in that class, looks like this:
TheproblemisthatwhenwelistallstudentswithinthePHPloop,tooglestopsworking.Seethecodebelow:
$c=0;while($jmListar=mysqli_fetch_object($sqlListar)){$listar.="<tr>";
$listar .= "<td>".$jmListar->NomeCompleto."</td>";
$listar .= "<td>Matemática</td>";
$listar .= "<td>
<div id=\"radioBtn\" class=\"btn-group\">
<a class=\"btn btn-primary btn-sm active\" data-toggle=\"presenca[".$c."]\" data-title=\"N\" style=\"border: 1px solid #000\">Não</a>
<a class=\"btn btn-danger btn-sm notActive\" data-toggle=\"presenca[".$c."]\" data-title=\"S\" style=\"border: 1px solid #000\">Sim</a>
</div>
<input type=\"hidden\" name=\"Faltou\" id=\"presenca\">
</td>";
$listar .= "</tr>";
$c++;
}
JQUERY
<script type="text/javascript">
$('#radioBtn a').on('click', function(){
var sel = $(this).data('title');
var tog = $(this).data('toggle');
$('#'+tog).prop('value', sel);
$('a[data-toggle="'+tog+'"]').not('[data-title="'+sel+'"]').removeClass('active').addClass('notActive');
$('a[data-toggle="'+tog+'"][data-title="'+sel+'"]').removeClass('notActive').addClass('active');
})
</script>