[英]“No results ” not working on filter search bar
I'm trying to make a filter search bar, and it works, but the "no results" page isn't.我正在尝试制作一个过滤搜索栏,它可以工作,但“无结果”页面却不行。 I've tried making the "no results" show up, but whatever I do, it doesn't.
我试过让“无结果”出现,但无论我做什么,它都没有。 (jquery/javascript) Thanks so much for the help: Here's my code:
(jquery/javascript) 非常感谢您的帮助:这是我的代码:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<div align="right"class="live-search-bar">
<input class="search-bar" id="searchbar" type="text" placeholder="Search for a game..">
</div>
<div id="button-container">
<div>
<button class="games-button">Oranges</button>
</div>
<div>
<button class="games-button">Bananas</button>
</div>
<div>
<button class="games-button">Apples</button>
</div>
<script>
$(document).ready(function(){
$("#searchbar").on("keyup", function() {
var value = $(this).val().toLowerCase();
$("#button-container button").filter(function() {
$(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
// No Results, Not working
$('.noresults').remove();
$("#button-container").each(function () {
if ($(this).children(':visible').length == 0)
$(this).append('<em>No Results</em>');
});
});
});
});
</script>
Here you go with a solution给你go一个解决办法
$(document).ready(function(){ $('.noresults').hide(); $("#searchbar").on("keyup", function() { var value = $(this).val().toLowerCase(); $("#button-container button").filter(function() { $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1) $('.noresults').hide(); var noResult = true; $("#button-container").children('div').each(function () { if ($(this).children(':visible').length;= 0) { noResult = false; } }). if (noResult) { $('.noresults');show(); } }); }); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div align="right"class="live-search-bar"> <input class="search-bar" id="searchbar" type="text" placeholder="Search for a game.."> </div> <div id="button-container"> <div> <button class="games-button">Oranges</button> </div> <div> <button class="games-button">Bananas</button> </div> <div> <button class="games-button">Apples</button> </div> </div> <div class="noresults"> No Results </div>
You need to loop through each div
with button-container
您需要使用
button-container
遍历每个div
$("#button-container").children('div').each(function () {});
Hope this will help you希望对你有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.