How to get checkbox and option value with jquery

Asked

Viewed 297 times

1

I have the following image below

inserir a descrição da imagem aqui

and each checkbox corresponds to the value

<input type="checkbox" value="3" class="serie2" id="serie2[3]" name="serie2[3]">
<input type="checkbox" value="4" class="serie2" id="serie2[4]" name="serie2[4]">
<input type="checkbox" value="5" class="serie2" id="serie2[5]" name="serie2[5]">

successively ... coming from php :

echo "<label><i><input type='checkbox' name='serie2[$lnSer[ser_cod]]' id='serie2[$lnSer[ser_cod]]' class='serie2' value='$lnSer[ser_cod]' $marca>$lnSer[ser_descr]</i></label>";

following, if in my Ajax function I set the variable like this : var serie = '5,17,14'; ok, more when I try to go through using the function below, the problem occurs:

var serie = [];
        $(".serie2:checked").each(function() {
        serie.push(this.value);
    });

how do I get the value in var serie = '5,17,14' ?

same option thing in option : inserir a descrição da imagem aqui

if I define the value as var stroke = 'I|25|TU-II|2|TU'; passes !

how do I get the value of this field, staying in this stroke format = 'I|25|TU-II|2|TU'

<option value="3|1|2">CICLOS E PRÁTICAS DOCENTES  | Turma 2 |   </option>
<option value="3|1|5">CICLOS E PRÁTICAS DOCENTES  | Turma 5 |   </option>

Thank you

  • At the end Voce wants the id of the selected checkboxes all in the same string separated by comma, and in the case of select are the ids separated by |, eh that?

  • have you tried using the "Join" function? It converts an array into a string, separating the elements by the argument sent to the function. variable.Join(", ");

2 answers

1

To get selected values from a <select>:

  $('select option:selected').each(function(){
      resultado = resultado +'|' + this.value;
  });

See the Jsfiddler working.

  • Thanks Thiago, it worked, I did for the course option: var curso0 = '; $('#curso0 option:Selected'). each(Function(){ curs0 += this.value; }); var curso1 = '; $('#curso1 option:Selected'). each(Function(){ curs1 += this.value; });

1

Even with the doubt I raised, I think I can propose a solution. Instead of accumulating the result, you can get a list of everything you want and then transform the results as you see fit. In the case of checkboxes:

var lista = $(".serie2")
              .filter(':checked')           // filtra pelos checados
              .map(function(idx, element) { // transforma a lista
                return element.value;       // nesse exemplo, uma lista de valores
              })

In the function Voce passes to the way map Voce specifies how to generate a new list, the way you want it. As for select, same principle:

var lista = $('select option:selected')
                .map(function(idx, el) { 
                    return el.value;
                })

Now Voce can create a string as it sees fit (type using join, as suggested), or anything else. The interesting thing is that so Oce is working declaratively instead of imperative :)

  • Thanks Juliano, but the error occurred when I implemented the check option Typeerror: 'stepup' called on an Object that does not implement interface Htmlinputelement.

  • This indicates that some element is being manipulated in the wrong way, like a jQuery object being used as a DOM Element

Browser other questions tagged

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