jQuery搜索多维ul列表

问题描述

| 我有下一个HTML:
  <input type=\"text\" id=\"search\" name=\"searchword\" value=\"\" />
<ul id=\"search_list\">
    <li>first</li>
    <li>second</li>
    <li>third
        <ul>
            <li>aaa</li>
            <li>bbbb
                <ul>
                    <li>111111</li>
                    <li>22222</li>
                    <li>3333</li>
                </ul>
            </li>
            <li>cccc</li>
        </ul>
    </li>
</ul>
我想通过输入搜索此列表。 我在输入中输入文字,并且不是搜索结果的
<li>
<ul>
\必须隐藏。 非常感谢您,我的英语不好

解决方法

根据您希望何时进行搜索和过滤,使用抠像,模糊或更改。
$(\'#search\').change(function() {
   var searchTerms = $(this).val();

   $(\'li\').each(function() {
      var hasMatch = searchTerms.length == 0 || $(this).is(\':contains(\' + searchTerms  + \')\');

      $(this).toggle(hasMatch);
   });
});
编辑:使解决方案稍微冗长一些,以便于阅读。 这是小提琴。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...