Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery-如果所有子项都受影响,则隐藏父项_Javascript_Jquery_Html - Fatal编程技术网

Javascript jQuery-如果所有子项都受影响,则隐藏父项

Javascript jQuery-如果所有子项都受影响,则隐藏父项,javascript,jquery,html,Javascript,Jquery,Html,我正在使用输入上的.keyup()为多个ul创建搜索功能。如果有匹配项,则显示.show()li和其他匹配项。hide() 如果内部的所有li都被隐藏了,我该如何爬上层次结构来隐藏整个ul HTML布局: <input type="text" class="brandsearch"> <ul class="brands-group"> <li>Adidas</li> <li>Nike</li> </ul&

我正在使用
输入上的
.keyup()
为多个
ul
创建搜索功能。如果有匹配项,则显示
.show()
li
和其他匹配项。
hide()

如果内部的所有
li
都被隐藏了,我该如何爬上层次结构来隐藏整个
ul

HTML布局:

<input type="text" class="brandsearch">

<ul class="brands-group">
   <li>Adidas</li>
   <li>Nike</li>
</ul>

  • 阿迪达斯
  • 耐克
您可以添加以下内容:

if (jQuery('.brands-group li:visible').length == 0) {
   jQuery('.brands-group ul').hide();
}
您可以添加以下内容:

if (jQuery('.brands-group li:visible').length == 0) {
   jQuery('.brands-group ul').hide();
}
jQuery('.brandsearch').keyup(function(event) {
    var inputValue = jQuery(".brandsearch").val().toLowerCase();
    jQuery(".brands-group li").each(function(event) {
         var $parent = jQuery(this).closest('ul');
         if(jQuery(this).html().toLowerCase().indexOf(inputValue) == -1)
         {
              jQuery(this).hide();
              if(jQuery('>li:visible', $parent).length == 0) $parent.hide();
         } else {
             jQuery(this).show();
             if($parent.is(':hidden')) $parent.show();
         }
    });
});