How to dynamic filter options of <select > with jQuery?

前端 未结 12 621
轮回少年
轮回少年 2020-11-29 18:54



        
12条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-29 19:18

    Much simpler code then most of the other solutions. Look for the text (case insensitive) and use CSS to hide/show the contents. Much better than storing a copy of the data.

    Pass into this method the id of the select box, and id of the input containing a filter.

    function FilterSelectList(selectListId, filterId)
    {
        var filter = $("#" + filterId).val();
        filter = filter.toUpperCase();
    
        var options = $("#" + selectListId + " option");
        for (var i = 0; i < options.length; i++)
        {
           if (options[i].text.toUpperCase().indexOf(filter) < 0)
               $(options[i]).css("display", "none");
           else
               $(options[i]).css("display", "block");
        }
    };
    

提交回复
热议问题