Handle jQuery array objects

Asked

Viewed 737 times

1

I have a read that receives various values, they have the same class. I need to take these values and pass to Hidden fields to send via $_POST to a PHP page. I was able to bring the data I need, but now 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>
<div id="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>

1 answer

1


You need to create the element and add to form:

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

Exit:

<input type="hidden" name="selecao[]" value="Conteudo 1">
<input type="hidden" name="selecao[]" value="Conteudo 2">
...

Browser other questions tagged

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