
本文档详细介绍了如何使用 HTML、CSS 和 JavaScript 创建一个动态可搜索的下拉列表,并实现选中项的显示功能。通过 JSON 数据动态生成下拉选项,并提供搜索过滤功能,最终将用户选择的条目信息展示出来。文章将提供完整的代码示例,并对关键步骤进行详细解释,帮助开发者快速掌握实现方法。
首先,我们需要创建一个包含下拉列表的 HTML 结构。这包括一个触发下拉列表显示的按钮和一个用于显示下拉选项的 div 容器。容器内包含一个输入框,用于搜索过滤选项。
<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>接下来,我们需要定义 CSS 样式来美化下拉列表,并控制其显示和隐藏。以下是一些关键的样式定义:
#myInput {
box-sizing: border-box;
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;
border: 1px solid #ddd;
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {
background-color: #f1f1f1;
}
.show {
display: block;
}
.dropdown-content > .dropdown-option{
cursor: pointer;
}
.dropdown-content > .selectedOption{
background-color: darkgray !important;
}现在,我们需要编写 JavaScript 代码来实现以下功能:
立即学习“Java免费学习笔记(深入)”;
* 从 JSON 数据动态生成下拉选项。 * 实现搜索过滤功能。 * 显示选中的选项。
function myFunction() {
var country = [
{
"ID": "001",
"Country_Name": "India"
},
{
"ID": "002",
"Country_Name": "Australia"
},
{
"ID": "003",
"Country_Name": "Austria"
},
{
"ID": "004",
"Country_Name": "China"
},
{
"ID": "005",
"Country_Name": "Bangladesh"
}
];
document.getElementById("myDropdown").classList.toggle("show");
var ele = document.getElementById("myDropdown");
//for each country in the country array
for (var i = 0; i < country.length; i++) {
//creates an anchor element
const countryOption = document.createElement("a");
//holding the country ID in its value attribute
countryOption.setAttribute('value', country[i]['ID']);
//and the country Name as its innerText
countryOption.innerText = country[i]['Country_Name'];
//adds the class dropdown-option to the option element
countryOption.classList.add('dropdown-option');
//binds the countryOnClick function defined below as its click event handler
countryOption.addEventListener('click', countryOnClick);
//appends the new anchor to the dropdown element
ele.appendChild(countryOption);
}
}
//click event handler for the dropdown options
function countryOnClick(event){
//fetch country properties from the element triggering the event
const clickedOption = event.target;
const countryID = clickedOption.getAttribute('value');
const countryName = clickedOption.innerText;
//removes the selectedOption class to every option in the dropdown
clickedOption.closest('div').querySelectorAll('.dropdown-option').forEach((o,i)=>{
o.classList.remove('selectedOption');
});
//adds the selectedOption class to the selected option element
clickedOption.classList.add('selectedOption');
//show those data on console
console.log(`${countryID} - ${countryName}`);
console.log( getSelectedOption() );
}
function getSelectedOption(){
return document.querySelector('#myDropdown > .dropdown-option.selectedOption');
}
//gets called when the filter input changes, and it filters the dropdown options list
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++) {
txtValue = a[i].textContent || a[i].innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
a[i].style.display = "";
} else {
a[i].style.display = "none";
}
}
}本文档详细介绍了如何使用 HTML、CSS 和 JavaScript 创建一个动态可搜索的下拉列表,并实现选中项的显示功能。通过 JSON 数据动态生成下拉选项,并提供搜索过滤功能,最终将用户选择的条目信息展示出来。希望本文档能够帮助你快速掌握实现方法。
以上就是使用 HTML、CSS 和 JavaScript 实现可搜索下拉列表并显示选中项的详细内容,更多请关注php中文网其它相关文章!
HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号