"No results "在过滤器搜索栏上不起作用



我正在尝试制作一个过滤器搜索栏,它可以工作,但"无结果"页面不行。我试着让"没有结果"出现,但无论我做什么,都没有。(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>

这里有一个解决方案

jsFiddle

$(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>

您需要使用button-container循环通过每个div

$("#button-container").children('div').each(function () {});

希望这能帮助你

最新更新