Filter multiple
    lists with jQuery

前端 未结 2 615
情书的邮戳
情书的邮戳 2021-02-05 09:51

I have multiple lists on one page (multiple categories of products) like this:

Category 1

  • item1<
2条回答
  •  伪装坚强ぢ
    2021-02-05 10:13

    You can do this with the filter method in jQuery:

    var valueToSearch = "item1"; // this one should come from the search box
    var allListItems = $("ul > li");
    var filteredItems = allListItems.filter(function(index) {
        var listItemValue = $(this).text();
        var hasText = listItemValue.indexOf(valueToSearch) > -1;
        return hasText;
    });
    

    Next, you can display the values in the filteredItems variable in a list or something like that, using a for loop.

提交回复
热议问题