如何通过 j.s 将过滤器添加到网站的搜索栏

问题描述 投票:0回答:1

我在搜索栏中有问题。 在一个页面中我至少有 30 个产品。 我会添加过滤器,当客户搜索产品名称时,产品显示。

我尝试创建过滤器,但无法编写正确的结构。 有没有人可以帮助我,我变得快乐。

javascript reactjs arrays json filter
1个回答
0
投票

如果 html 列表中列出了产品,您可以尝试以下代码

function myFunction() {
  // Declare variables
  var input, filter, ul, li, a, i, txtValue;
  input = document.getElementById('myInput');
  filter = input.value.toUpperCase();
  ul = document.getElementById("myUL");
  li = ul.getElementsByTagName('li');

  // Loop through all list items, and hide those who don't match the search query
  for (i = 0; i < li.length; i++) {
    a = li[i].getElementsByTagName("a")[0];
    txtValue = a.textContent || a.innerText;
    if (txtValue.toUpperCase().indexOf(filter) > -1) {
      li[i].style.display = "";
    } else {
      li[i].style.display = "none";
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.