[英]Hide and show a div based on search input
I have been trying to hide the titleMain
div when there is any input in the search field.当搜索字段中有任何输入时,我一直试图隐藏
titleMain
div。 But for some reason, it isn't working.但由于某种原因,它不起作用。
I have been trying to show the titleMain
div when there is no input in the search and hidden when there is any search input.我一直在尝试在搜索中没有输入时显示
titleMain
div,并在有任何搜索输入时隐藏。
const searchBar = document.forms['search-books'].querySelector('input'); searchBar.addEventListener('keyup', function(e) { const term = e.target.value.toLocaleLowerCase(); const books = document.getElementsByTagName('h5'); var notAvailable = document.getElementById('notAvailable'); var hasResults = false; Array.from(books).forEach(function(book) { const title = book.textContent; if (title.toLowerCase().indexOf(term) != -1) { book.parentElement.parentElement.style.display = 'flex'; hasResults = true; } else { book.parentElement.parentElement.style.display = 'none'; } }); notAvailable.style.display = hasResults ? 'none' : 'block'; }); if ($('#search').val.length == 0) { $("#titleMain").show(); } else { $("#titleMain").hide(); }
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/> <div class="container" id="titleMain" style="background-color: red;">Book List</div> <form id="search-books"> <input type="text" placeholder="Search a book ... "> </form> <div class="container"> <div class="row list-single"> <div class="col-2"><img src="https://images.gr-assets.com/books/1447303603s/2767052.jpg"/></div> <div class="col-10"> <h5> The Hunger Games</h5> <a href="The-Hunger-Games.html">Learn</a> </div> </div> <br> <div class="row list-single"> <div class="col-2"><img src="https://images.gr-assets.com/books/1507396732s/2.jpg"/></div> <div class="col-10"> <h5>Harry Potter</h5> <a href="Harry-Potter.html">Learn</a> </div> </div> <div class="row list-single" id="notAvailable" style="display: none;"> <div class="col-12"> <h5>Sorry, the book has not been added yet</h5> </div> </div> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
You need to put the check for your search input in the event handler.您需要在事件处理程序中检查您的搜索输入。 You're also not checking the input's value itself.
您也没有检查输入的值本身。 Use
if ($('input').val().length == 0) {
.使用
if ($('input').val().length == 0) {
。 You can make the show/hide a one liner with $("#titleMain").toggle($('input').val().length == 0);
您可以使用
$("#titleMain").toggle($('input').val().length == 0);
显示/隐藏$("#titleMain").toggle($('input').val().length == 0);
: :
Example:示例:
const searchBar = document.forms['search-books'].querySelector('input'); searchBar.addEventListener('keyup', function(e) { const term = e.target.value.toLocaleLowerCase(); const books = document.getElementsByTagName('h5'); var notAvailable = document.getElementById('notAvailable'); $("#titleMain").toggle($('input').val().length == 0); var hasResults = false; Array.from(books).forEach(function(book) { const title = book.textContent; if (title.toLowerCase().indexOf(term) != -1) { book.parentElement.parentElement.style.display = 'flex'; hasResults = true; } else { book.parentElement.parentElement.style.display = 'none'; } }); notAvailable.style.display = hasResults ? 'none' : 'block'; });
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" /> <div class="container" id="titleMain" style="background-color: red;">Book List</div> <form id="search-books"> <input type="text" placeholder="Search a book ... "> </form> <div class="container"> <div class="row list-single"> <div class="col-2"><img src="https://images.gr-assets.com/books/1447303603s/2767052.jpg" /></div> <div class="col-10"> <h5> The Hunger Games</h5> <a href="The-Hunger-Games.html">Learn</a> </div> </div> <br> <div class="row list-single"> <div class="col-2"><img src="https://images.gr-assets.com/books/1507396732s/2.jpg" /></div> <div class="col-10"> <h5>Harry Potter</h5> <a href="Harry-Potter.html">Learn</a> </div> </div> <div class="row list-single" id="notAvailable" style="display: none;"> <div class="col-12"> <h5>Sorry, the book has not been added yet</h5> </div> </div> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
You just need to move your showing and hiding code into the event listener, so that it runs every time the input changes.您只需要将显示和隐藏代码移动到事件侦听器中,以便在每次输入更改时运行。
Looks correct apart from that.
除此之外看起来是正确的。
val
is a method which you need to call - $('#search').val().length
. val
是一种您需要调用的方法 - $('#search').val().length
。 $('#search').val.length
will give you the number of arguments expected by val
- Function.length
. $('#search').val.length
将为您提供val
- Function.length
预期的参数数量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.