1
In my HTML you have the following line:
<label class="turnoCurso pretoClaro" id="3"> Vespertino, Noturno </label>
I need to turn the turns that are brought from the database into an array and for that I use the following code:
var turnoLista = $('#'+id+'.turnoCurso').text().trim();
turnoLista = turnoLista.split(",")
And so far so good, the problem is to leave the checkbox as checked it just marks the first item and ignores the others, follow the code I’m using
$(turnoLista).each(function(indice, valor){ //
switch(valor)
{
case "Vespertino":
$("#turnoV").prop('checked',true);
break;
case "Matutino":
$("#turnoM").prop('checked',true);
break;
case "Noturno":
$("#turnoN").prop('checked',true);
break;
case "EAD":
$("#turnoE").prop('checked',true);
break;
}
});
Checkbox:
<input id="turnoM" type="checkbox" name="turno" value="1"> Matutino
<input id="turnoV" type="checkbox" name="turno" value="2"> Vespertino
<input id="turnoN" type="checkbox" name="turno" value="3"> Noturno
<input id="turnoE" type="checkbox" name="turno" value="4"> EaD
What happens is that the code only leaves the first chebox checked and if you put an Alert inside each it can detect all vector indices correctly, someone would know how to help me ?
I’m using a JS that is embedded into the page through an Ajax request and so I can’t debug the JS.
Notice that between "Vespertino" and "Nocturne" there is a space beyond the comma! You should have a more generic split like
.split(/[, ]/)
. You can put the Ajax code?– Sergio