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

128
Vistas
HTML Dropdown filter based on another dropdown selection

I tried dropdown filter based on another dropdown, It's working If I don't use space in the value in option. If I use space in value, then it's not working.

$("#Type").on("change", function() {
  var values = $(this).val().split(',', ) //split value which is selected
  $("#fruits option").hide() //hide all options from slect box
  //loop through values
  for (var i = 0; i < values.length; i++) {
    var vals = values[i]
    $("#fruits option[value=" + vals + "]").show() //show that option

  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="form-row last">
  <div class="form-wrapper">
    <label for="">Department</label>
    <select id="Type" name="Type" class='form-control'>
      <option disabled="disabled" selected="selected">Choose option</option>
      <option value='Apple'>Diet 1</option>
      <option value='Red Banana,Green Apple'>Diet 2</option>
    </select>
    <i class="zmdi zmdi-chevron-down"></i>
  </div>

  <div class="form-wrapper">
    <label for="">Device</label>
    <select id="fruits" name="fruits" class='form-control'>
      <option disabled="disabled" selected="selected">Choose option</option>
      <option value='Apple'>Apple</option>
      <option value='Red Banana'>Banana</option>
      <option value='Green Apple'>G Apple</option>
    </select>
    <i class="zmdi zmdi-chevron-down"></i>
  </div>
</div>

Here If I select Apple value - Diet 1, it's working. If I select Diet 2, it should show Banana and G Apple in second drop down. Please help me how to get the Red Banana value from 1st dropdown and filter the second dropdown,

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You weren't far off you just missing closing single quotation marts in this line:

before:

 $("#fruits option[value=" + vals + "]").show() /

after

 $("#fruits option[value='" + vals + "']").show() /

$("#Type").on("change", function() {
debugger
  var values = $(this).val().split(',', ) //split value which is selected
  $("#fruits option").hide() //hide all options from slect box
  //loop through values
  for (var i = 0; i < values.length; i++) {
    var vals = values[i]
    $("#fruits option[value='" + vals + "']").show() //show that option

  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="form-row last">
  <div class="form-wrapper">
    <label for="">Department</label>
    <select id="Type" name="Type" class='form-control'>
      <option disabled="disabled" selected="selected">Choose option</option>
      <option value='Apple'>Diet 1</option>
      <option value='Red Banana,Green Apple'>Diet 2</option>
    </select>
    <i class="zmdi zmdi-chevron-down"></i>
  </div>

  <div class="form-wrapper">
    <label for="">Device</label>
    <select id="fruits" name="fruits" class='form-control'>
      <option disabled="disabled" selected="selected">Choose option</option>
      <option value='Apple'>Apple</option>
      <option value='Red Banana'>Banana</option>
      <option value='Green Apple'>G Apple</option>
    </select>
    <i class="zmdi zmdi-chevron-down"></i>
  </div>
</div>

I hope this helps

about 4 years ago · Juan Pablo Isaza Denunciar

0

Biased on you follow up question here is an example of the second drop down bound to an array instead of being hard coded

var data = [{
  'value': 'Apple',
  'product': 'Green Apple'
}, {
  'value': 'Grape',
  'product': 'Fresh Grape'
}];

$(function() {
  $.each(data, function(index, itemData) {
    $('#fruits').append($("<option></option>")
      .attr("value", itemData.product)
      .text(itemData.value));
  });
})

$("#Type").on("change", function() {

  var values = $(this).val().split(',', ) //split value which is selected
  $("#fruits option").hide() //hide all options from slect box
  //loop through values
  for (var i = 0; i < values.length; i++) {
    var vals = values[i]
    $("#fruits option[value='" + vals + "']").show() //show that option

  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="form-row last">
  <div class="form-wrapper">
    <label for="">Department</label>
    <select id="Type" name="Type" class='form-control'>
      <option disabled="disabled" selected="selected">Choose option</option>
      <option value='Fresh Grape'>Diet 1</option>
      <option value='Blue Apple,Green Apple'>Diet 2</option>
    </select>
    <i class="zmdi zmdi-chevron-down"></i>
  </div>

  <div class="form-wrapper">
    <label for="">Device</label>
    <select id="fruits" name="fruits" class='form-control'>
      <option disabled="disabled" selected="selected">Choose option</option>
 
    </select>
    <i class="zmdi zmdi-chevron-down"></i>
  </div>
</div>

I hope this helps

about 4 years ago · Juan Pablo Isaza 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