選項過多(如城市、國家、產品)時,原生 <select> 難以查找。需支援輸入關鍵字即時過濾選項。
<datalist>(零依賴、語意化)<input list="cities" placeholder="輸入城市名">
<datalist id="cities">
<option value="台北市"></option>
<option value="新北市"></option>
<option value="桃園市"></option>
<option value="台中市"></option>
<option value="高雄市"></option>
</datalist>
限制:樣式難自訂、無法強制只能選列表值、大量選項效能差。
<select id="city" name="city"></select>
<script src="https://cdn.jsdelivr.net/npm/choices.js/public/assets/scripts/choices.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/choices.js/public/assets/styles/choices.min.css">
<script>
new Choices('#city', {
searchEnabled: true,
removeItemButton: true,
placeholder: true,
placeholderValue: '搜尋或選擇城市',
itemSelectText: ''
});
</script>
<div class="custom-select">
<input type="text" class="select-input" placeholder="搜尋..." autocomplete="off" readonly>
<span class="select-arrow">▼</span>
<ul class="select-options" hidden></ul>
<input type="hidden" name="city_id">
</div>
<style>
.custom-select { position: relative; width: 300px; font-family: sans-serif; }
.select-input { width: 100%; padding: 8px 32px 8px 12px; border: 1px solid #ccc; border-radius: 4px; box-sizing: border-box; }
.select-arrow { position: absolute; right: 10px; top: 50%; transform: translateY(-50%); pointer-events: none; }
.select-options { position: absolute; top: 100%; left: 0; right: 0; max-height: 200px; overflow-y: auto; border: 1px solid #ccc; border-top: none; background: #fff; margin: 0; padding: 0; list-style: none; z-index: 100; }
.select-options li { padding: 8px 12px; cursor: pointer; }
.select-options li:hover, .select-options li.highlight { background: #eef; }
.select-options[hidden] { display: none; }
</style>
<script>
(function() {
var data = [
{id: 1, name: '台北市'}, {id: 2, name: '新北市'}, {id: 3, name: '桃園市'},
{id: 4, name: '台中市'}, {id: 5, name: '台南市'}, {id: 6, name: '高雄市'},
{id: 7, name: '基隆市'}, {id: 8, name: '新竹市'}, {id: 9, name: '嘉義市'}
];
var $wrap = document.querySelector('.custom-select');
var $input = $wrap.querySelector('.select-input');
var $list = $wrap.querySelector('.select-options');
var $hidden = $wrap.querySelector('input[type=hidden]');
var highlighted = -1;
function render(filter) {
$list.innerHTML = '';
var filtered = data.filter(function(item) {
return item.name.includes(filter);
});
filtered.forEach(function(item, i) {
var li = document.createElement('li');
li.textContent = item.name;
li.dataset.id = item.id;
li.dataset.index = i;
$list.appendChild(li);
});
highlighted = -1;
}
function highlight(idx) {
var items = $list.querySelectorAll('li');
items.forEach(function(li, i) { li.classList.toggle('highlight', i === idx); });
highlighted = idx;
}
$input.addEventListener('focus', function() { render(''); $list.hidden = false; });
$input.addEventListener('input', function() { render(this.value); });
$input.addEventListener('keydown', function(e) {
var items = $list.querySelectorAll('li');
if (e.key === 'ArrowDown') { e.preventDefault(); highlight(Math.min(highlighted + 1, items.length - 1)); }
else if (e.key === 'ArrowUp') { e.preventDefault(); highlight(Math.max(highlighted - 1, 0)); }
else if (e.key === 'Enter') { e.preventDefault(); if (highlighted >= 0) selectItem(items[highlighted]); }
else if (e.key === 'Escape') { close(); }
});
$list.addEventListener('click', function(e) {
if (e.target.tagName === 'LI') selectItem(e.target);
});
document.addEventListener('click', function(e) {
if (!$wrap.contains(e.target)) close();
});
function selectItem(li) {
$input.value = li.textContent;
$hidden.value = li.dataset.id;
close();
}
function close() { $list.hidden = true; $input.blur(); }
render('');
})();
</script>
role="combobox"、aria-expanded、aria-controls、aria-activedescendant| 方案 | 體積 | 功能 | 維護成本 | 推薦場景 |
|---|---|---|---|---|
<datalist> | 0 KB | 基礎 | 無 | 簡單、選項<50、無樣式需求 |
| Choices.js | ~30 KB | 完整 | 低 | 生產專案、多選、分組、遠端搜尋 |
| Tom Select | ~25 KB | 完整 | 低 | 無 jQuery 依賴、效能佳 |
| 自製 | ~3 KB | 可控 | 高 | 極度客製化、學習用途 |