Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

253
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda