javascript - 搜索/过滤下拉列表:单击时更改值变量
问题描述
我刚刚按照本指南实施了搜索/过滤器下拉菜单:https ://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_js_dropdown_filter 。
<h2>Search/Filter Dropdown</h2>
<p>Click on the button to open the dropdown menu, and use the input field to search for a specific dropdown link.</p>
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
<div id="myDropdown" class="dropdown-content">
<input type="text" placeholder="Search.." id="myInput" onkeyup="filterFunction()">
</div>
</div>
这个 Search / Filter Dropdown 将 allNameMuseums() 方法的数组作为值,即 ["ACQUARIUM", "Museo2", "Museo3"]。
document.addEventListener("DOMContentLoaded", function(event) {
allNameMuseums().forEach(function(item) {
document.getElementById("myDropdown").innerHTML += '<a href="' + item + '">' + item + '</a>';
})
});
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
function filterFunction() {
var input, filter, ul, li, a, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
div = document.getElementById("myDropdown");
a = div.getElementsByTagName("a");
for (i = 0; i < a.length; i++) {
if (a[i].innerHTML.toUpperCase().indexOf(filter) > -1) {
a[i].style.display = "";
} else {
a[i].style.display = "none";
}
}
}
function allNameMuseums() {
return ["ACQUARIO", "Museo2", "Museo3"];
}
我有两个异步方法,specificoMuseo(name) 和 allMuseums(),以及一个var data = proof;
获取 allMuseums() 方法结果的变量。
specificoMuseo(name).then(proof2 => {
allMuseums().then(proof => {
var data = proof;
});
});
我希望当我点击搜索/过滤器下拉菜单的 ACQUARIO 时,变量“数据”将作为特定的Museo(“ACQUARIO”)的结果的输入。
解决方案
您需要类似的逻辑如下,
var name; //globle variable so you can access in your method
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
function fnclick(obj) {
name = obj.innerHTML;
alert(name);
console.log(name);
}
function filterFunction() {
var input, filter, ul, li, a, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
div = document.getElementById("myDropdown");
a = div.getElementsByTagName("a");
for (i = 0; i < a.length; i++) {
if (a[i].innerHTML.toUpperCase().indexOf(filter) > -1) {
a[i].style.display = "";
} else {
a[i].style.display = "none";
}
}
}
.dropbtn {
background-color: #4CAF50;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropbtn:hover, .dropbtn:focus {
background-color: #3e8e41;
}
#myInput {
border-box: box-sizing;
background-image: url('searchicon.png');
background-position: 14px 12px;
background-repeat: no-repeat;
font-size: 16px;
padding: 14px 20px 12px 45px;
border: none;
border-bottom: 1px solid #ddd;
}
#myInput:focus {outline: 3px solid #ddd;}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f6f6f6;
min-width: 230px;
overflow: auto;
border: 1px solid #ddd;
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown a:hover {background-color: #ddd;}
.show {display: block;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h2>Search/Filter Dropdown</h2>
<p>Click on the button to open the dropdown menu, and use the input field to search for a specific dropdown link.</p>
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
<div id="myDropdown" class="dropdown-content">
<input type="text" placeholder="Search.." id="myInput" onkeyup="filterFunction()">
<a href="#about" onclick="fnclick(this)">About</a>
<a href="#base" onclick="fnclick(this)">Base</a>
<a href="#blog" onclick="fnclick(this)">Blog</a>
</div>
</div>
推荐阅读
- maven - 限制 jenkins maven 作业在多云设置中的特定云上运行
- python - IRAF 转换任务 - 错误(741,“无法打开文件(数据库)”)
- android - 无论如何在运行时 ReactNative 中更改 SplashScreen 吗?
- javascript - 如何避免 D3.js 中的形状重叠,同时将每个形状都关联到特定的 x 和 y 位置
- javascript - '无法读取 nuxtJs/firebase 中未定义的属性'应用程序'
- typescript - Typescript typegoose getModelForClass 返回类型
- java - HSQL:将其作为 HQSL 数据库引擎服务器运行?
- python - 无法在 Flask ApiRest 上使用“user_id”作为参数发出请求
- python - 显示函数接近某个值
- javascript - 表带模型对象属性为空,但调试整个对象显示很多属性