Handle jQuery array objects

1

I have a li that receives several values, they have the same class. I need to get these values and move to hidden fields to send via $ _POST to a PHP page. I've been able to get the data I need, but I need to pass this data one by one.

$(function() {

  var x = [];

  $('#selecao .active').each(function() {

    x.push($(this).val());

  });

  $('.p').text(x);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script><divid="selecao">
  <input type="text" class="active" value="Conteudo 1" />
  <input type="text" class="active" value="Conteudo 2" />
  <input type="text" class="active" value="Conteudo 3" />
  <input type="text" class="active" value="Conteudo 4" />
  <input type="text" class="active" value="Conteudo 5" />
  <input type="text" class="active" value="Conteudo 6" />
  <input type="text" class="active" value="Conteudo 7" />
  <input type="text" class="active" value="Conteudo 8" />
  <input type="text" class="active" value="Conteudo 9" />
  <input type="text" class="active" value="Conteudo 10" />
  <p class="p"></p>
</div>
    
asked by anonymous 03.08.2015 / 23:23

1 answer

1

You need to create the element and add it to form :

$(function() {
    $('#selecao .active').each(function() {
        $('<input/>', {
            type: 'hidden',
            name: 'selecao[]',
            value: $(this).val()
        }).appendTo('#form');
    });
});

Output:

<input type="hidden" name="selecao[]" value="Conteudo 1">
<input type="hidden" name="selecao[]" value="Conteudo 2">
...
    
04.08.2015 / 00:07