当用户点击搜索结果中的标题时,Focusout功能可防止用户重定向到标题页.每当我点击页面上的不同元素时,它就会按预期关闭搜索结果,但每当我点击搜索结果时,搜索结果也会关闭.
我try 使用blur函数以及Hide()和show(),但得到了相同的结果.我知道我有.Result Display None,但这不应该阻止我点击搜索结果?这是怎么回事?
// jquery.js
$(document).ready(function() {
$('.search-box input[type="text"]').on("keyup input", function() {
/* Get input value on change */
var inputVal = $(this).val();
var resultDropdown = $(this).siblings(".result");
if (inputVal.length) {
$.get("includes/searchbar.inc.php", {
term: inputVal
}).done(function(data) {
// Display the returned data in browser
resultDropdown.html(data);
$('.result').css('display', 'block');
$("#searchboxy").focusout(function() {
$('.result').css('display', 'none');
});
$("#searchboxy").focusin(function() {
$('.result').css('display', 'block');
});
});
} else {
resultDropdown.empty();
}
});
// Set search input value on click of result item
$(document).on("click", ".result", function() {
$(this).parents(".search-box").find('input[type="text"]').val($(this).text());
$(this).parent(".result").empty();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- header.php -->
<div class="search-box" id="searchboxy">
<input type="text" autocomplete="off" placeholder="Search for a game or genre..." />
<div class="result" id="resultsearch"></div>
</div>