Insert option in select Multiple dynamically

Asked

Viewed 272 times

0

Good Morning!!!

I need that when typing a process number and clicking on the magnifying glass, the process goes to the select Multiple box, as image attached. But I need this process several times and populate select Multiple and then click the select button and these selected values appear in a text box on the main screen, because this box is a modal. Hj can only insert one process, if I search for another process and click on the magnifying glass, it replaces what is already in the box, instead of adding.

inserir a descrição da imagem aqui

Grateful.

  • And what shape are you using to add the process to select?

  • Put the code you are using to set the select

1 answer

0

We can put a code that will add the values of input in the select by clicking on the magnifying glass, and then doing something with the content. The code looks like this:

$("#vai").click(function() {
  var processo_val = $("#processo").val();
  $("#select").append(
    $("<option/>")
    .val(processo_val)
    .text(processo_val)
  );
  $("#processo").val("").focus();
});

$("#ok").click(function() {
  var processos = [];
  $("#select option").each(function(idx, el) {
    processos.push($(el).val());
  });
  alert(JSON.stringify(processos));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<strong>Processo:</strong> <input id="processo" type="text"> <button id="vai">adicionar!</button><br><br>
<select id="select" multiple style="width: 200px;">
</select>

<hr>

<button id="ok" style="font-size:16pt;">Clique aqui para fazer algo com os processos!</button>

Browser other questions tagged

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