
本文详细介绍了如何使用 HTML, CSS 和 JavaScript 创建一个动态下拉列表,该列表从 JSON 数组中获取数据,并允许用户搜索和选择项目。文章重点讲解了如何捕获用户选择的项目,并将其显示在页面上,同时提供代码示例和注意事项,帮助开发者快速实现该功能。
HTML 结构
首先,我们需要定义 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 样式
立即学习“Java免费学习笔记(深入)”;
接下来,我们需要使用 CSS 来美化下拉列表的外观。以下代码展示了一些基本的 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;
cursor: pointer; /* 添加鼠标悬停样式 */
}
.dropdown-content a:hover {
background-color: #f1f1f1;
}
.show {
display: block;
}
.dropdown-content > .dropdown-option{
cursor: pointer;
}
.dropdown-content > .selectedOption{
background-color: darkgray !important;
}JavaScript 逻辑
现在,我们需要使用 JavaScript 来实现下拉列表的动态功能。以下代码展示了如何从 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" }
];
var dropdown = document.getElementById("myDropdown");
dropdown.classList.toggle("show");
dropdown.innerHTML = ''; // Clear existing content
// Add search input
let input = document.createElement('input');
input.type = 'text';
input.placeholder = 'Search...';
input.id = 'myInput';
input.onkeyup = filterFunction;
dropdown.appendChild(input);
// Populate dropdown with country options
for (var i = 0; i < country.length; i++) {
const countryOption = document.createElement("a");
countryOption.setAttribute('value', country[i]['ID']);
countryOption.innerText = country[i]['Country_Name'];
countryOption.classList.add('dropdown-option');
countryOption.addEventListener('click', countryOnClick);
dropdown.appendChild(countryOption);
}
}
function countryOnClick(event) {
const clickedOption = event.target;
const countryID = clickedOption.getAttribute('value');
const countryName = clickedOption.innerText;
// Remove selectedOption class from all options
clickedOption.closest('div').querySelectorAll('.dropdown-option').forEach((o, i) => {
o.classList.remove('selectedOption');
});
// Add selectedOption class to the selected option
clickedOption.classList.add('selectedOption');
// Log selected country details
console.log(`${countryID} - ${countryName}`);
console.log(getSelectedOption());
// You can further update the UI to display the selected country
// For example, update a <span> element with id 'selectedCountry'
// document.getElementById('selectedCountry').innerText = countryName;
}
function getSelectedOption() {
return document.querySelector('#myDropdown > .dropdown-option.selectedOption');
}
function filterFunction() {
var input, filter, 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 创建一个动态下拉列表,并实现搜索和选择功能。通过本文的学习,你可以快速创建一个功能完善的下拉列表,并将其应用到你的项目中。 记住,代码只是一个起点,根据你的具体需求进行修改和扩展才是关键。
以上就是使用 HTML, CSS 和 JavaScript 显示下拉列表中选中的项目的详细内容,更多请关注php中文网其它相关文章!
HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号