javascript - 清除搜索输入时将建议附加到搜索框
问题描述
我有一个具有自动完成功能的搜索栏。首先点击搜索输入,所有建议都被添加,然后我可以搜索一个特定的单词,但是当我清除搜索输入(删除输入字段)时,所有建议都应该显示,但在清除搜索时列表显示为空输入标签
我该如何解决这个问题?
var proj_names=[]
recent_content={'projects':[{'project_name':'test1'},{'project_name':'test2'},{'project_name':'test3'}]}
recent_content['projects'].map((val)=>{
proj_names.push(val["project_name"])
$('.results').append(`<li class='search-list'>`+val["project_name"]+`</li>`)
})
document.getElementById('myInput').addEventListener('input', (e)=>{
let projArray = [];
if(e.target.value){
projArray = proj_names.filter(proj => proj.toLowerCase().includes(e.target.value));
projArray = projArray.map(proj_names => `<li class='search-list'>${proj_names}</li>`)
}
showprojectArray(projArray);
});
function showprojectArray(projArray){
const html = !projArray.length ? '' : projArray.join('');
document.querySelector('.results').innerHTML = html;
}
#custom-search-input {
margin:0;
margin-top: 10px;
padding: 0;
}
.form-control{
border-radius: 0;
}
#custom-search-input .search-query {
padding-right: 3px;
padding-right: 4px \9;
padding-left: 3px;
padding-left: 4px \9;
border: 1px solid #494949 ;
/* border-right-style: none; */
margin-bottom: 0;
}
#custom-search-input button {
background-color:rgb(0,0,0,0.3) ;
height: calc(1.5em + .75rem + 2px);
padding: 2px 10px;
position: relative;
vertical-align: middle;
}
.ion-android-search{
font-size: 150%;
}
.btn-danger{
border:none;
border-radius: 0;
}
.search-query:focus + button {
z-index: 3;
}
.search-form {
width: 100%;
display: flex;
}
.search-form input:focus + .results { display: block }
.search-form .results {
display: none;
position: absolute;
top: 40px;
left: 0;
cursor: pointer;
right: 0;
z-index: 10;
padding: 0;
margin: 0;
/* margin-top: 5px; */
border-width: 1px;
border-style: solid;
border-color: #cbcfe2 #c8cee7 #c4c7d7;
border-radius: 3px;
background-color: #fdfdfd;
background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #fdfdfd), color-stop(100%, #eceef4));
background-image: -webkit-linear-gradient(top, #fdfdfd, #eceef4);
background-image: -moz-linear-gradient(top, #fdfdfd, #eceef4);
background-image: -ms-linear-gradient(top, #fdfdfd, #eceef4);
background-image: -o-linear-gradient(top, #fdfdfd, #eceef4);
background-image: linear-gradient(top, #fdfdfd, #eceef4);
-webkit-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
-moz-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
-ms-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
-o-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}
.search-form .results li { display: block }
.search-form .results li:first-child { margin-top: -1px }
.search-form .results li:first-child:before, .search-form .results li:first-child:after {
display: block;
content: '';
width: 0;
height: 0;
position: absolute;
left: 50%;
margin-left: -5px;
border: 5px outset transparent;
}
.search-form .results li:first-child:before {
border-bottom: 5px solid #c4c7d7;
top: -2px;
}
.search-form .results li:first-child:after {
border-bottom: 5px solid #fdfdfd;
top: -10px;
}
.search-form .results li:first-child:hover:before, .search .results li:first-child:hover:after { display: none }
.search-form .results li:last-child { margin-bottom: -1px }
.search-form .results li {
display: block;
position: relative;
margin: 0 -1px;
padding: 6px 40px 6px 10px;
color: #808394;
font-weight: 500;
text-shadow: 0 1px #fff;
border: 1px solid transparent;
border-radius: 3px;
}
.search-form .results li span { font-weight: 200 }
.search-form .results li:before {
content: '';
width: 18px;
height: 18px;
position: absolute;
top: 50%;
right: 10px;
margin-top: -9px;
/* background: url("https://cssdeck.com/uploads/media/items/7/7BNkBjd.png") 0 0 no-repeat; */
}
.search-form .results li:hover {
text-decoration: none;
color: #fff;
text-shadow: 0 -1px rgba(0, 0, 0, 0.3);
border-color: #28be9a #28be9a #28be9a;
background-color: #28be9a;
-webkit-box-shadow: inset 0 1px rgba(255, 255, 255, 0.2), 0 1px rgba(0, 0, 0, 0.08);
-moz-box-shadow: inset 0 1px rgba(255, 255, 255, 0.2), 0 1px rgba(0, 0, 0, 0.08);
-ms-box-shadow: inset 0 1px rgba(255, 255, 255, 0.2), 0 1px rgba(0, 0, 0, 0.08);
-o-box-shadow: inset 0 1px rgba(255, 255, 255, 0.2), 0 1px rgba(0, 0, 0, 0.08);
box-shadow: inset 0 1px rgba(255, 255, 255, 0.2), 0 1px rgba(0, 0, 0, 0.08);
}
:-moz-placeholder {
color: #a7aabc;
font-weight: 200;
}
::-webkit-input-placeholder {
color: #a7aabc;
font-weight: 200;
}
.lt-ie9 .search input { line-height: 26px }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="custom-search-input">
<div class="input-group col-md-12 ">
<form class="search-form">
<input type="text" id="myInput" class=" search-query form-control search-input" placeholder="Search" />
<ul class="results" >
</ul>
</form>
</div>
</div>
解决方案
您需要为 event.target.value 案例编写 else 块。
if(e.target.value){
projArray = proj_names.filter(proj => proj.toLowerCase().includes(e.target.value));
projArray = projArray.map(proj_names => `<li class='search-list'>${proj_names}</li>`)
} else {
// when e.target.value is empty show entire array.i.e. proj_names
projArray = proj_names.map(proj_name => return `<li>${proj_name}</li>`)
}
推荐阅读
- bash - 如何用bash嵌套for循环输出单个替代?
- mapbox-gl-js - 将 openmaptiles.org 底图样式与 Deck.gl 一起使用
- javascript - 正则表达式斜杠不必要的转义字符
- android - 自定义 NavController 和自定义 NavHost
- python - 瓶子返回响应非常缓慢
- php - 将多维数组拆分为多个数组
- javascript - javacsript中字符串中的正则表达式数学双引号字符串是什么?
- excel - 检测excel气泡图气泡是否重叠或确定确切的气泡大小
- python-2.7 - 多处理和作为输出的列表返回错误:分配前引用的局部变量“”
- javascript - 如何使用 onClick 为同一个按钮设置多个功能?