JavaScript 计数过滤变量上的匹配项

标签 javascript

我正在构建一个应用程序,您可以在其中过滤所有业务合作伙伴。我已经成功地做到了这一点。现在我试图显示匹配数量。

看看这个,我想我无法过滤已经过滤的变量,这是正确的吗?

How can I display the matches in an filtered variable?

我尝试过滤过滤结果并计算 true bool 值

    results = a.innerHTML.toUpperCase().indexOf(filter) > -1;
    result = document.getElementById("result");

    var count = results.filter(function(s) { return s.value; }).length;
    console.log("#True: " + count)

    result.innerHTML = results.length;

function myFunction() {
    // Declare variables
    var input, filter, ul, li, a, i, results, result;
    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];

        
        if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
            li[i].style.display = "";
        } else {
            li[i].style.display = "none";
        }
    }
}
* {
  box-sizing: border-box;
}

#myInput {
  background-image: url('/css/searchicon.png');
  background-position: 10px 12px;
  background-repeat: no-repeat;
  width: 100%;
  font-size: 16px;
  padding: 12px 20px 12px 40px;
  border: 1px solid #ddd;
  margin-bottom: 12px;
}

#myUL {
  list-style-type: none;
  padding: 0;
  margin: 0;
}

#myUL li a {
  border: 1px solid #ddd;
  margin-top: -1px; /* Prevent double borders */
  background-color: #f6f6f6;
  padding: 12px;
  text-decoration: none;
  font-size: 18px;
  color: black;
  display: block
}

#myUL li a:hover:not(.header) {
  background-color: #eee;
}
<h2>My Phonebook</h2>

<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name">

<ul id="myUL">
  <li><a href="#">
    <b>Contact Name:</b><h3>Adele</h3><br>
    <b>Contact Company:</b><h3>james</h3><br>
    <b>Contact Phone:</b><h3>frank</h3><br>
    <b>Contact Business:</b><h3>rupert</h3></a>
  </li>
  
  <li><a href="#">
    <b>Contact Name:</b><h3>Bert</h3><br>
    <b>Contact Company:</b><h3>Jordan</h3><br>
    <b>Contact Phone:</b><h3>Peele</h3><br>
    <b>Contact Business:</b><h3>Gunter</h3></a>
  </li>

    
  <li><a href="#">
  <b>Contact Name:</b><h3>Adele</h3><br>
  <b>Contact Company:</b><h3>Lubert</h3><br>
  <b>Contact Phone:</b><h3>Agny</h3><br>
  <b>Contact Business:</b><h3>Loki</h3></a></li>

    
  <li><a href="#">
    <b>Contact Name:</b><h3>Thor</h3><br>
    <b>Contact Company:</b><h3>Robert</h3><br>
    <b>Contact Phone:</b><h3>frank</h3><br>
    <b>Contact Business:</b><h3>Chris</h3></a>
  </li>

    
  <li><a href="#">
    <b>Contact Name:</b><h3>Sean</h3><br>
    <b>Contact Company:</b><h3>Shawn</h3><br>
    <b>Contact Phone:</b><h3>Shuan</h3><br>
    <b>Contact Business:</b><h3>rupert</h3></a>
  </li>

    
  <li><a href="#">
    <b>Contact Name:</b><h3>Johhny</h3><br>
    <b>Contact Company:</b><h3>Urkel</h3><br>
    <b>Contact Phone:</b><h3>Uruk</h3><br>
    <b>Contact Business:</b><h3>Derk</h3></a>
  </li>
</ul>

最佳答案

为什么不直接声明一个像 filterCount 这样的数字,并在每次过滤结果时递增它呢?

请参阅下面的代码片段。

function myFunction() {
    // Declare variables
    var input, filter, ul, li, a, i, results, result, matches = 0;
    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];

        
        if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
            matches++;
            li[i].style.display = "";
        } else {            
            li[i].style.display = "none";
        }
    }
    // displaying the number of filtered results
    document.getElementById("matchesCount").innerHTML = matches;
    
    // showing the label in case at least one result is filtered
    if (matches < li.length) {
      document.getElementById("showNumberMatches").style.display = "";
    } else {
      document.getElementById("showNumberMatches").style.display = "none";
    }
}
* {
  box-sizing: border-box;
}

#myInput {
  background-image: url('/css/searchicon.png');
  background-position: 10px 12px;
  background-repeat: no-repeat;
  width: 100%;
  font-size: 16px;
  padding: 12px 20px 12px 40px;
  border: 1px solid #ddd;
  margin-bottom: 12px;
}

#myUL {
  list-style-type: none;
  padding: 0;
  margin: 0;
}

#myUL li a {
  border: 1px solid #ddd;
  margin-top: -1px; /* Prevent double borders */
  background-color: #f6f6f6;
  padding: 12px;
  text-decoration: none;
  font-size: 18px;
  color: black;
  display: block
}

#myUL li a:hover:not(.header) {
  background-color: #eee;
}
<h2>My Phonebook</h2> <span id="showNumberMatches" style="display:none"><span id="matchesCount"></span> results match your query</span>

<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name">

<ul id="myUL">
  <li><a href="#">
    <b>Contact Name:</b><h3>Adele</h3><br>
    <b>Contact Company:</b><h3>james</h3><br>
    <b>Contact Phone:</b><h3>frank</h3><br>
    <b>Contact Business:</b><h3>rupert</h3></a>
  </li>
  
  <li><a href="#">
    <b>Contact Name:</b><h3>Bert</h3><br>
    <b>Contact Company:</b><h3>Jordan</h3><br>
    <b>Contact Phone:</b><h3>Peele</h3><br>
    <b>Contact Business:</b><h3>Gunter</h3></a>
  </li>

    
  <li><a href="#">
  <b>Contact Name:</b><h3>Adele</h3><br>
  <b>Contact Company:</b><h3>Lubert</h3><br>
  <b>Contact Phone:</b><h3>Agny</h3><br>
  <b>Contact Business:</b><h3>Loki</h3></a></li>

    
  <li><a href="#">
    <b>Contact Name:</b><h3>Thor</h3><br>
    <b>Contact Company:</b><h3>Robert</h3><br>
    <b>Contact Phone:</b><h3>frank</h3><br>
    <b>Contact Business:</b><h3>Chris</h3></a>
  </li>

    
  <li><a href="#">
    <b>Contact Name:</b><h3>Sean</h3><br>
    <b>Contact Company:</b><h3>Shawn</h3><br>
    <b>Contact Phone:</b><h3>Shuan</h3><br>
    <b>Contact Business:</b><h3>rupert</h3></a>
  </li>

    
  <li><a href="#">
    <b>Contact Name:</b><h3>Johhny</h3><br>
    <b>Contact Company:</b><h3>Urkel</h3><br>
    <b>Contact Phone:</b><h3>Uruk</h3><br>
    <b>Contact Business:</b><h3>Derk</h3></a>
  </li>
</ul>

关于JavaScript 计数过滤变量上的匹配项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49795258/

相关文章:

javascript - 当我打印arguments.callee.caller在IE浏览器以外的浏览器中为null时,结果为null

javascript - JSON 解析问题

javascript - 使用 REGEX 匹配用户输入

javascript - 单击时如何从错误类中删除错误消息

javascript - 如何在 Vue 中将 v-for 索引传递给子级?

php - 代码点火器输入

javascript - 如何在字符串中查找数字并将其替换为 dom 对象?

javascript - 按钮行为异常并重新加载页面

javascript - 如何使用计划日期和时间触发此示例?

javascript - 将文本区域文本转换为 Html,并为每个新行添加新的 <p> 标记