YL

FORM & INPUT

自动补全输入Combobox / Autocomplete

输入时下方浮出匹配项列表,选中项回填输入框——输入和选择二合一。

中英对照

中文
自动补全输入
英文
Combobox / Autocomplete
别名
下拉联想、自动完成、Combo Box
一句话
输入时下方浮出匹配项列表,选中项回填输入框——输入和选择二合一。

效果预览

北3 个匹配
北北京 · 中国
北北九州 · 日本
北北岸 · 新西兰

适合用在哪

城市选择、标签输入、成员选择、搜索任何有限集合。

不适合:选项集合无限或必须跳转搜索结果页时:Combobox 扛不住;选项带复杂多字段对比时,改用表格选择或专用选择器。

和相近模式的区别

搜索下拉高亮Search with Highlighted Results

搜索下拉强调命中高亮与「查看全部」;Combobox 强调从有限集合过滤并回填输入框,选中即完成。

标签输入框Tag Input

Tag Input 生成多个标签;Combobox 通常单选回填,也可作为「从推荐生成标签」的数据源。

容易踩的坑

  • 没有 role="combobox" 和 aria-activedescendant,方向键移动时读屏不播报当前选项。
  • 每敲一个字就发请求不做防抖,网络慢时旧结果覆盖新结果出现错乱。
  • 面板贴近视口底部时不翻转到上方,移动端被软键盘遮住看不到选项。

设计提示词 · Prompt

自动补全输入(Combobox / Autocomplete):输入框本身在聚焦时显示品牌描边 + 3px 光晕;下方 4px 处浮出结果面板(白底、1px 描边、8px 圆角、大投影、与输入框同宽),列表项含图标 + 文字,键盘 ↑↓ 导航时当前项用品牌实心反白(与鼠标悬停同样式),Enter 选中。过滤逻辑高亮命中字符(品牌色加粗)。面板出现用 120ms 淡入 + 4px 上移。列表最多显示 7 项后内部滚动。空结果也要有面板:"没有找到 xxx,换个关键词试试"。
Combobox / Autocomplete: when focused, the input itself shows a brand-color border + 3px glow; a results panel floats 4px below (white background, 1px border, 8px border radius, large shadow, same width as the input). List items contain an icon + text; when navigating with the ↑↓ keys, the current item is shown inverted on solid brand color (same style as mouse hover), and Enter selects it. Filtering highlights matched characters (brand color, bold). The panel appears with a 120ms fade-in + 4px upward shift. The list shows at most 7 items before scrolling internally. Empty results still need a panel: "No results for xxx, try another keyword".

可复制 CSS

自包含的最小实现,粘贴到空白页面即可生效;类名可以按项目规范改名。

/* 输入框 + 下方结果面板 */
.combo { position: relative; width: min(360px, 100%); }
.combo__input {
  display: flex;
  align-items: center;
  height: 44px;
  padding: 0 12px;
  border: 1px solid #4f46e5;
  border-radius: 10px;
  background: #fff;
  box-shadow: 0 0 0 3px rgba(79, 70, 229, .12);
  font: 15px/1 system-ui, sans-serif;
}
.combo__list {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  z-index: 20;
  max-height: calc(7 * 40px);
  overflow: auto;
  padding: 6px;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 16px 40px -12px rgba(0, 0, 0, .18);
  animation: combo-in .12s ease-out;
}
.combo__item {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 40px;
  padding: 0 10px;
  border-radius: 6px;
  font: 14px/1.3 system-ui, sans-serif;
  color: #111827;
  cursor: pointer;
}
.combo__item.is-on,
.combo__item:hover {
  background: #4f46e5;
  color: #fff;
}
.combo__empty {
  padding: 12px 10px;
  font: 13px/1.4 system-ui, sans-serif;
  color: #6b7280;
}
@keyframes combo-in {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}
@media (prefers-reduced-motion: reduce) {
  .combo__list { animation: none; }
}

演示 HTML 结构

<div class="s d-combo">
  <div class="d-combo__input">北<span class="d-combo__cur">3 个匹配</span></div>
  <div class="d-combo__list">
    <div class="d-combo__item is-on"><span class="d-av d-av--sm d-av--sq">北</span>北京 · 中国</div>
    <div class="d-combo__item"><span class="d-av d-av--sm d-av--sq">北</span>北九州 · 日本</div>
    <div class="d-combo__item"><span class="d-av d-av--sm d-av--sq">北</span>北岸 · 新西兰</div>
  </div>
</div>

无障碍要点

  • 遵循 Combobox 模式:aria-expanded、aria-controls、aria-activedescendant;↑↓ 移动,Enter 选中,Esc 关闭。
  • 空结果也要留上面板,避免焦点丢失。

常见问题

自动补全输入的英文是什么?

自动补全输入的英文是 Combobox / Autocomplete。常见英文别名还包括 Combo Box。

Combobox / Autocomplete 的中文是什么?

Combobox / Autocomplete 的中文是「自动补全输入」。常见中文别名还包括 下拉联想、自动完成。输入时下方浮出匹配项列表,选中项回填输入框——输入和选择二合一。

面板最多显示几项?

最多约 7 项后内部滚动(示例 max-height 按 7×40px)。更多结果应引导去完整搜索页,而不是无限拉长下拉。

命中字符怎么高亮?

用品牌色加粗包裹匹配片段即可;键盘当前项与鼠标悬停用同一套实心反白,避免两套样式。

常见搭配

同一页面里经常和「自动补全输入」一起出现的模式,可以收藏后用「组合收藏为整页提示词」一起交给 AI。

标签