查询搜索框以查找div

时间:2015-11-06 10:13:57

标签: javascript jquery

HELP!我花了一天时间试图解决这个问题。

// search function
$(document).ready(function(){
    $("#bc").hide();
    $("#exerciseSearch").keyup(function(){
 
        // Retrieve the input field text and reset the count to zero
        var filter = $(this).val(), count = 0;

 
        // Loop through the comment list
        $("#exercisesCont div").each(function(){
 
            // If the list item does not contain the text phrase fade it out
            if ($(this).text().search(new RegExp(filter, "i")) < 0) {
                $(this).fadeOut();

 
            // Show the list item if the phrase matches and increase the count by 1
            } else {
                $(this).show();
                count++;
            }
        });
 
        // Update the count
        var numberItems = count;
        $("#filter-count").text("Number of Comments = "+count);
    });

});
<input type="text" class="form-control text-input" id="exerciseSearch" value="">
<div id='exercisesCont'>
  <div id='1'>
    <div>1</div>
    <div>Turning this div to display:none</div>
  </div>
  <div id='2'>
    <div>2</div>
    <div>Turning this div to display:none</div>
  </div>    
  <div id='3'>
    <div>3</div>
    <div>Turning this div to display:none</div>
  </div>
  <div id='4'>
    <div>4</div>
    <div>Turning this div to display:none</div>
  </div>
</div>

目前,当我使用搜索功能搜索“1”时,我得到“1”的结果。这听起来很棒,但它适用于div 1的子元素中的“text”。

我需要的是搜索Div ID(在div id'exerciseCont'中),而不是搜索'text'并显示div及其所有子元素(当前兄弟元素正在转向display:none)。

我希望这有道理吗?提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

试试这个

array_map(function($v) {
            return count(array_unique($v));
        }, $arr);