Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

252
Visualizações
How to keep multiple selected values from dropdown after form submission

I am trying to keep the values selected after form submission, when the user selects more than one value is should still be selected after the submit button has been clicked.

<?php
$example = $_POST["name1"];
?>   

<form action="" method="post">
<select id="id1" name="name1" class="selectpicker" title="Select Currencies" multiple="multiple">
<option <?php if (isset($example) && $example=="a") echo "selected";?>>A</option>
<option <?php if (isset($example) && $example=="b") echo "selected";?>>B</option>
<option <?php if (isset($example) && $example=="c") echo "selected";?>>C</option>
</select>

<button name="submit" type="submit" value="Submit">SUBMIT</button>
</form>



<script>

$('#id1').on('change', function() {

var values = $(this).val()
$("#id2 option").hide() //hide all options
$('#id2').selectpicker('deselectAll') //if want to remove all selcted optn

if (values.length > 0) {
for (var i = 0; i < values.length; i++) {
  //show where value is same..
  $("#id2 option[value=" + values[i] + "]").show()
}
} else {
$("#id2 option").show() //show all options 
}
$('#id2').selectpicker('refresh'); //refresh selctpicker

});


</script>
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Use pure JS

var options = document.getElementById('id1').options;
for (let i = 0; i < options.length; i++) { 
  console.log(options[i].value);//log the value
}
OR use jquery

$("#id1 option").each(function(){
    var thisOptionValue=$(this).val();
});

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda