How to select multiple checkboxes from Array ?

Asked

Viewed 384 times

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?

1 answer

1


Javascript native foreach solution.

First observation

You may have mistaken the order of the values within the callback function, I tested it here and the signature of the foreach method ( native to javascript ) is:

elementos.forEach(function(valor_atual,indice_atual){}

With this you should take the first argument ( current value_value ).

Second observation

At the time you do the value treatment that is on the label was not complete to be treated correctly within the foreach.

The label text value is:

value_do_label = ' Vespertino, Nocturne ';

Note that you have 1 space before Vespetino and other spaces before and after the string after the comma.

This should make two treatments.

  • In the general string to remove spaces before and after.
  • Inside each element of the list, to remove the spaces that may have been between the separate ( "," ) and each element.

Would look like this:

elementos = valor_do_label.trim().split(",")

And within the iteration on the list:

elementos.forEach(function(valor,indice){

    valor = valor.trim(); // Aqui removendo os espaços remanescentes que podem quebrar a verificação.

    switch(valor){
        case "Vespetino":
             $("#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;
    }
})
  • 1

    The problem really was in the TRIM friend, although it used the Trim in var turnoLista = $('#'+id+'.turnoCurso'). text(). Trim() for some reason the spaces were not cleaned correctly, after changing the split to turnoLista = turnoLista.Trim(). split(",") and the switch variable for value.Trim() started to work correctly, the Each I’m using is the Jquery and the operation really is that way (Indice,valor) I checked this before with an Alert, thank you for the force I didn’t even imagine.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.