How to get the value of the checkbox and option with jquery

1

I have the following image below

andeachcheckboxcorrespondstothevalue

<inputtype="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 ... 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>";

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

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

How do I get the value in the format var var = '5,17,14'?

same thing option in option:

IfIsetthevalueasvarcourse='I|25|TU-II|2|TU';Pass!

HowdoIgetthevalueofthevalueofthisfield,gettingthatformatcourse='I|25|TU-II|2|TU'

<optionvalue="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

    
asked by anonymous 23.02.2016 / 16:18

2 answers

1

To get selected values from a <select> :

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

See JsFiddler working.

    
23.02.2016 / 17:00
1

Even with the doubt that I raised I think I can propose a solution. Instead of accumulating the result, you can get a list with everything you want and then transform the results in the way you think appropriate. 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 that you pass to the map method, you specify how to generate a new list, any way you want. As for select, same principle:

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

Now you can create a string in the way you think is appropriate (type using join , as suggested), or anything else. The interesting thing is that you are working declaratively rather than imperative:)

    
23.02.2016 / 18:00