javascript-如果在JQUERY中keyup为空,如何显示div

前端之家收集整理的这篇文章主要介绍了javascript-如果在JQUERY中keyup为空,如何显示div 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有一个代码,显示搜索框中输入的div内容.

  1. $(document).ready(function(){
  2. $("#myInput").on("keyup",function() { //#myinput is the search Box
  3. var value = $(this).val().toLowerCase();
  4. //#myList is the content that displays the searched data.
  5. $("#myList .card").filter(function() {
  6. $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
  7. });
  8. });
  9. });

现在我要显示这个div

  1. <div class="alert alert-warning text-center" id="emptyList" style="display:none;">
  2. <strong>No data found!</strong> You can add product by clicking the button "Add item" below or <a href="addItem.PHP">click here</a>
  3. </div>

如果过滤器为空.

我是jquery新手,所以你们可以在这方面帮助我.

最佳答案
您可以使用:visible伪选择器添加对可见卡片的检查,例如:

  1. $(document).ready(function() {
  2. $("#myInput").on("keyup",function() { //#myinput is the search Box
  3. var value = $(this).val().toLowerCase();
  4. //#myList is the content that display the searched data.
  5. $("#myList .card").filter(function() {
  6. $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
  7. });
  8. if (!$("#myList .card:visible").length) {
  9. $('#emptyList').show();
  10. } else {
  11. $('#emptyList').hide();
  12. }
  13. });
  14. });

shorhand版本可以这样写:

  1. $('#emptyList').toggle(!$("#myList .card:visible").length);

猜你在找的HTML相关文章