Filter Category Select Drop down based on Another drop down option

前端 未结 4 1004
盖世英雄少女心
盖世英雄少女心 2021-01-05 07:54

I want to filter the list of options based on the selection of another drop down.

Please see the jquery code below; i am sure there is a tiny bit that i am missing t

4条回答
  •  旧时难觅i
    2021-01-05 08:07

    Here is my approach of adding/removing options based on the options selected and this will work in most of browsers.

    I have modified the html by adding class to first select options like

    
    
    

    JS:

    $(document).ready(function () {    
        var allOptions = $('#selectprod option')
        $('#selectcat').change(function () {
            $('#selectprod option').remove(); //remove all options
            var classN = $('#selectcat option:selected').prop('class'); //get the 
            var opts = allOptions.filter('.' + classN); //selected option's classname
            $.each(opts, function (i, j) {
                $(j).appendTo('#selectprod'); //append those options back
            });
        });
    });
    

    JSFiddle

提交回复
热议问题