jquery - Nested dropdown lists : depending values -


i have 2 dropdown lists values. when select value in first, want return elements same selected value in second. second list depending of first's list selection. how ?

<div class="form-group">     <label for="first">first list</label>         <select id="first" class="form-control" role="listbox" onchange="filterlist();">             <option value="select level 1" selected="selected">select...</option>             <option value="option 1">option 1</option>             <option value="option 2">option 2</option>         </select> </div> <div class="form-group">     <label for="second">second list</label>         <select id="second" class="form-control" role="listbox">             <option value="select level 2" data-group="select" selected="selected">select...</option>             <option value="option 1 - 1" data-group="option 1">first list 1 - element 1</option>             <option value="option 1 - 2" data-group="option 1">first list 1 - element 2</option>             <option value="option 2 - 1" data-group="option 2">first list 2 - element 1</option>             <option value="option 2 - 2" data-group="option 2">first list 2 - element 2</option>         </select> </div> 

jquery script

function filterlist(){      var first = $("#first").find('option:selected').text(); // stores first list selected elements      $("#option-container").children().appendto("#second"); // moves <option> contained in #option-container <select>      var tomove = $("#second").children("[data-group!='"+first+"']"); // selects elements move out      tomove.appendto("#option-container"); // moves elements in #option-container      $("#second").removeattr("disabled"); // enables select }; 

you can hide option setting css-property display none, jquery's $.hide() function does. or can set disabled-attribute on element ($.attr('disabled', 'disabled')) make option unselectable.

edit

a small example of (though haven't tested it):

$('#first').change(function() {     var value = $(this).val();     $('#second').children().attr('disabled', 'disabled');     $('#second').children('[data-group=' + value + ']').removeattr('disabled'); }); 

this each time in first dropdown selected, disable options in second dropdown , re-enable options in second dropdown data-group-attribute corresponding selected value first dropdown.


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -