【问题标题】:"No results " not working on filter search bar“无结果”不适用于过滤器搜索栏
【发布时间】:2020-07-17 06:13:24
【问题描述】:

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

【问题讨论】:

  • tr 添加到div 不是可行的方法。只需从em 部分开始。

标签: javascript jquery html search


【解决方案1】:

给你一个解决方案

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 () {});

希望对你有帮助

【讨论】:

  • 这很完美!谢谢,我现在意识到我做错了什么。
猜你喜欢
  • 1970-01-01
  • 2021-11-22
  • 2018-12-10
  • 2011-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多