清除框字段后如何删除“无结果”消息

时间:2020-08-14 13:24:58

标签: javascript html jquery search filter

我当前正在使用以下代码创建过滤器列表。用户尝试搜索某项内容后,出现无结果消息,这对我来说是正确的。如果用户从搜索框中删除字母,并且该框为空,则如何隐藏“无结果”消息。我是否需要在Java脚本中添加一些额外的代码,或者可以在CSS代码区域中完成此操作?

function myFunction_search() {
  var input, filter, ul, li, a, i;
  input = document.getElementById("myInput");
  filter = input.value.toUpperCase();
  ul = document.getElementById("myUL");
  li = ul.getElementsByTagName("li");
  var found = false;

  for (i = 0; i < li.length; i++) {

    a = li[i].getElementsByTagName("a")[0];
    if (filter !== "" && a.innerHTML.toUpperCase().indexOf(filter) > -1) {

      li[i].style.display = "block";
      found = true;
    } else {

      li[i].style.display = "none";
    }
  }
  document.getElementById("no_result_msg").setAttribute("style", "display:" + (found ? 'none' : 'block'))
}
* {
  box-sizing: border-box;
}

#myInput {
  background-image: url('images/search-icon.png');
  background-position: 4px 4px;
  background-repeat: no-repeat;
  width: 100%;
  font-size: 12px;
  padding: 8px 8px 8px 40px;
  border: 1px solid #ddd;
  margin-bottom: 12px;
}

#myUL {
  list-style-type: none;
  padding: 0;
  margin: 0;
    position: absolute;
  z-index: 2;
}
    
#no_result_msg {
  display: none;
    
    }       

#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 {
  display: none;
    }

#myUL li a:hover:not(.header) {
  background-color: #eee;
}
<input type="text" id="myInput" onkeyup="myFunction_search()" placeholder="Search for product names or product codes.." title="Type in a name">
<ul id="myUL">
  <li><a href="#">Adele</a></li>
  <li><a href="#">Agnes</a></li>

  <li><a href="#">Billy</a></li>
  <li><a href="#">Bob</a></li>

  <li><a href="#">Calvin</a></li>
  <li><a href="#">Christina</a></li>
  <li><a href="#">Cindy</a></li>
</ul>

<div class="no_result_msg" id="no_result_msg"><i><u>No Results found</u></i></div> 

2 个答案:

答案 0 :(得分:1)

您可以让div包含该消息,然后根据是否找到搜索结果显示或隐藏它。

function myFunction() {
  var input, filter, ul, li, a, i;
  input = document.getElementById("myInput");
  filter = input.value.toUpperCase();
  ul = document.getElementById("myUL");
  li = ul.getElementsByTagName("li");
  var found = false;

  for (i = 0; i < li.length; i++) {

    a = li[i].getElementsByTagName("a")[0];
    if (filter !== "" && a.innerHTML.toUpperCase().indexOf(filter) > -1) {

      li[i].style.display = "block";
      found = true;
    } else {

      li[i].style.display = "none";
    }
  }
  document.getElementById("no_result_msg").setAttribute("style", "display:" + (found ? 'none' : 'block'))
}
* {
  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 {
  display: none;
}

#myUL li a:hover:not(.header) {
  background-color: #eee;
}
<!DOCTYPE html>
<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

<body>

  <h2>My Phonebook</h2>

  <input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name">
  <ul id="myUL">
    <div id='no_result_msg'>No Results found</div>
    <li><a href="www.google.com">Adele</a></li>
    <li><a href="#">Agnes</a></li>
    <li><a href="#">Billy</a></li>
    <li><a href="#">Bob</a></li>
    <li><a href="#">Calvin</a></li>
    <li><a href="#">Christina</a></li>
    <li><a href="#">Cindy</a></li>

  </ul>

</body>

</html>

答案 1 :(得分:0)

尝试一下

function myFunction() {
        var input, filter, ul, li, i, c, a = false;
        input = document.getElementById("myInput");
        no_result = document.getElementById("no_result_found")
        filter = input.value.toUpperCase();
        ul = document.getElementById("myUL");
        li = ul.getElementsByTagName("li");
        for (i = 0; i < li.length; i++) {
            c = li[i].getElementsByTagName("a")[0];
            if (c.innerHTML.toUpperCase().indexOf(filter) !== -1) {
                a = true;
            }
            if (filter !== "" && a) {
                no_result.setAttribute("style", "display: none")
                li[i].style.display = "block";
            } else {
                li[i].style.display = "none";
            }
        }
        if (!a) {
            no_result.setAttribute("style", "display: block")
        }
    }
* {
  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 {
  display: none;
    }

#myUL li a:hover:not(.header) {
  background-color: #eee;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>

<h2>My Phonebook</h2>

<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name">
<ul id="myUL">
<div id='no_result_found' style="display: none;">No result found</div>
  <li><a href="www.google.com">Adele</a></li>
  <li><a href="#">Agnes</a></li>
  <li><a href="#">Billy</a></li>
  <li><a href="#">Bob</a></li>
  <li><a href="#">Calvin</a></li>
  <li><a href="#">Christina</a></li>
  <li><a href="#">Cindy</a></li>
  
</ul>

</body>
</html>