当搜索字段中有任何输入时,我一直试图隐藏titleMain
div。但由于某种原因,它无法正常工作。
我一直试图在搜索中没有输入时显示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>
答案 0 :(得分:1)
您只需将显示和隐藏代码移动到事件侦听器中,以便每次输入更改时它都会运行。 除此之外看起来正确。 val
是一种需要调用的方法 - $('#search').val().length
。 $('#search').val.length
将为您提供val
- Function.length
预期的参数数量。
答案 1 :(得分:1)
您需要在事件处理程序中检查搜索输入。您也没有检查输入值本身。使用if ($('input').val().length == 0) {
。您可以使用$("#titleMain").toggle($('input').val().length == 0);
显示/隐藏单行内容:
示例:
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>