Pregunta

mi código:

<select id="select">
<option id="1" value="thai language">option one</option>
<option id="2" value="eng language">option two</option>
<option id="3" value="other language">option three</option>
</select>

<div id="form1">content here</div>
<div id="form2">content here</div>
<div id="form3">content here</div>

lo que quiero es mostrar div # form1 cuando seleccione la opción 1 y oculte form2 + form3, o seleccione la opción 2 mostrar div # form2 y ocultar form1 + form2

¿Fue útil?

Solución

$('#select').change(function() {
   $('#form1, #form2, #form3').hide();
   $('#form' + $(this).find('option:selected').attr('id')).show();
});

Tenga en cuenta que los ID no deberían comenzar con números, pero lo anterior debería hacerlo.

Otros consejos

Si sus formularios son grandes, puede ponerlos en archivos separados como este,

$(document).ready(function() {
     $('#select').change(function() {
         $("#myform").load(this.value);
     });
 });


<select id="select">
<option value="blank.htm">Select A Form</option>
<option value="test1.htm">option one</option>
<option value="test2.htm">option two</option>
<option value="test3.htm">option three</option>
</select>

<div id="myform" ></div>

¿Algo como esto?

var optionValue = $("#select").val();

$('#form1, #form2, #form3').hide();

switch(optionValue)
{
case 1:
  $("#form1").show();
  break;
case 2:
  $("#form2").show();
  break;
case: 3:
  $("#form3").show();
  break;
}

¿No sería mejor ocultar solo el div mostrado anteriormente? Entonces;

var selection = 0;
$('#select').change(function() {
  $('#form' + selection).hide();
  selection = $(this).val();
  $('#form' + selection).show();
});

Tenga en cuenta que los ID no deberían comenzar con números, pero lo anterior debería hacerlo.

Mejor versión:

$('#select').change(function() {
   $('div').not('#form' + $(this).find('option:selected').attr('id')).hide();
   $('#form' + $(this).find('option:selected').attr('id')).show();
});
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top