gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 02:06:13 27 4
gpt4 key购买 nike

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

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

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com