要实现一个用JavaScript写的Select支持上下键、首字母筛选以及回车取值的功能可以分为以下几个步骤:
要实现一个用JavaScript写的Select支持上下键、首字母筛选以及回车取值的功能可以分为以下几个步骤:
- 创建一个select元素并隐藏原生的下拉列表。可以使用CSS的display:none或者position:absolute; left:-9999px; 等方法来实现。
<select id="mySelect" style="display:none;">
<option value="opt1">Option 1</option>
<option value="opt2">Option 2</option>
<option value="opt3">Option 3</option>
<option value="opt4">Option 4</option>
<option value="opt5">Option 5</option>
</select>
- 创建一个文本框用来模拟下拉列表,并在其下方插入一个显示选项的列表框。同时在文本框的keydown事件中添加上下键和回车键的事件处理函数。
<div style="position:relative;">
<input id="mySelectTxt" placeholder="请选择" />
<ul id="mySelectList"></ul>
</div>
const mySelect = document.getElementById('mySelect');
const mySelectTxt = document.getElementById('mySelectTxt');
const mySelectList = document.getElementById('mySelectList');
let options = [];
for (let opt of mySelect.options) {
options.push(opt.textContent);
}
function filterOptions(keyword) {
return options.filter(o => o.toLowerCase().startsWith(keyword.toLowerCase()));
}
function updateList(keyword) {
const filteredOptions = filterOptions(keyword);
mySelectList.innerHTML = '';
for (let opt of filteredOptions) {
const li = document.createElement('li');
li.textContent = opt;
li.addEventListener('click', () => {
mySelectTxt.value = opt;
mySelect.focus();
mySelectList.innerHTML = '';
});
mySelectList.appendChild(li);
}
}
mySelectTxt.addEventListener('keydown', e => {
if (e.keyCode === 38) { // Up arrow
if (mySelectList.childNodes.length > 0) {
const selected = mySelectList.querySelector('.selected');
if (selected) {
if (selected.previousSibling) {
selected.classList.remove('selected');
selected.previousSibling.classList.add('selected');
}
} else {
mySelectList.lastChild.classList.add('selected');
}
}
} else if (e.keyCode === 40) { // Down arrow
if (mySelectList.childNodes.length > 0) {
const selected = mySelectList.querySelector('.selected');
if (selected) {
if (selected.nextSibling) {
selected.classList.remove('selected');
selected.nextSibling.classList.add('selected');
}
} else {
mySelectList.firstChild.classList.add('selected');
}
}
} else if (e.keyCode === 13) { // Enter
const selected = mySelectList.querySelector('.selected');
if (selected) {
mySelectTxt.value = selected.textContent;
}
mySelect.focus();
mySelectList.innerHTML = '';
}
});
mySelectTxt.addEventListener('input', e => {
updateList(e.target.value);
});
- 在文本框的input事件中筛选出符合输入框内内容的下拉列表选项并展示出来。可以使用字符串的indexOf或正则表达式等方式来搜索符合条件的选项,并展示出来。
function filterOptions(keyword) {
return options.filter(o => o.toLowerCase().startsWith(keyword.toLowerCase()));
}
function updateList(keyword) {
const filteredOptions = filterOptions(keyword);
mySelectList.innerHTML = '';
for (let opt of filteredOptions) {
const li = document.createElement('li');
li.textContent = opt;
li.addEventListener('click', () => {
mySelectTxt.value = opt;
mySelect.focus();
mySelectList.innerHTML = '';
});
mySelectList.appendChild(li);
}
}
mySelectTxt.addEventListener('input', e => {
updateList(e.target.value);
});
示例1:
<select id="mySelect" style="display:none;">
<option value="1">China</option>
<option value="2">Japan</option>
<option value="3">South Korea</option>
<option value="4">Thailand</option>
<option value="5">Indonesia</option>
<option value="6">Vietnam</option>
<option value="7">Malaysia</option>
<option value="8">Singapore</option>
<option value="9">Philippines</option>
</select>
<div style="position:relative;">
<input id="mySelectTxt" placeholder="请选择" />
<ul id="mySelectList"></ul>
</div>
<script>
const mySelect = document.getElementById('mySelect');
const mySelectTxt = document.getElementById('mySelectTxt');
const mySelectList = document.getElementById('mySelectList');
let options = [];
for (let opt of mySelect.options) {
options.push(opt.textContent);
}
function filterOptions(keyword) {
return options.filter(o => o.toLowerCase().startsWith(keyword.toLowerCase()));
}
function updateList(keyword) {
const filteredOptions = filterOptions(keyword);
mySelectList.innerHTML = '';
for (let opt of filteredOptions) {
const li = document.createElement('li');
li.textContent = opt;
li.addEventListener('click', () => {
mySelectTxt.value = opt;
mySelect.focus();
mySelectList.innerHTML = '';
});
mySelectList.appendChild(li);
}
}
mySelectTxt.addEventListener('input', e => {
updateList(e.target.value);
});
</script>
示例2:
<select id="mySelect" style="display:none;">
<option value="option1">Red</option>
<option value="option2">Green</option>
<option value="option3">Blue</option>
<option value="option4">Yellow</option>
<option value="option5">Black</option>
<option value="option6">White</option>
</select>
<div style="position:relative;">
<input id="mySelectTxt" placeholder="请选择" />
<ul id="mySelectList"></ul>
</div>
<script>
const mySelect = document.getElementById('mySelect');
const mySelectTxt = document.getElementById('mySelectTxt');
const mySelectList = document.getElementById('mySelectList');
let options = [];
for (let opt of mySelect.options) {
options.push(opt.textContent);
}
function filterOptions(keyword) {
return options.filter(o => o.toLowerCase().startsWith(keyword.toLowerCase()));
}
function updateList(keyword) {
const filteredOptions = filterOptions(keyword);
mySelectList.innerHTML = '';
for (let opt of filteredOptions) {
const li = document.createElement('li');
li.textContent = opt;
li.addEventListener('click', () => {
mySelectTxt.value = opt;
mySelect.focus();
mySelectList.innerHTML = '';
});
mySelectList.appendChild(li);
}
}
mySelectTxt.addEventListener('input', e => {
updateList(e.target.value);
});
</script>
沃梦达教程
本文标题为:一个用javascript写的select支持上下键、首字母筛选以及回车取值的功能
猜你喜欢
- BOOTSTRAP时间控件显示在模态框下面的bug修复 2024-01-03
- layui open值获取及数据回显 2022-10-05
- cocos creator游戏之弹窗动画的实现 2022-10-29
- Ajax 动态载入html页面后不能执行其中的js快速解决方法 2023-02-15
- 什么是BFC? CSS 使用伪元素清除浮动的方法 2024-01-02
- 一篇文章弄清楚Ajax请求的五个步骤 2023-02-24
- Express无法通过req.body获取请求传递的数据解决方法 2024-01-15
- Javascript和Java语言有什么关系?两种语言间的异同比较 2023-12-24
- Typescript + Vue + Eslint使用不报错的方法总结。 2023-10-08
- css Sub-Pixel Bug?! 2023-12-15