jQuery 带有输入字段的 HTML 选择下拉列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15992085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:09:36  来源:igfitidea点击:

HTML select drop-down with an input field

javascriptjqueryhtml

提问by jesal

How can I implement a select-style drop down menu in HTML with an option that is a text input?

如何使用文本输入选项在 HTML 中实现选择样式的下拉菜单?

The idea being that someone can either select from a list of predefined options, or input their own option.

这个想法是有人可以从预定义选项列表中进行选择,或者输入他们自己的选项。

回答by Oleg

You can use input text with "list" attribute, which refers to the datalist of values.

您可以使用带有“list”属性的输入文本,该属性指的是值的数据列表。

<input type="text" name="city" list="cityname">
    <datalist id="cityname">
      <option value="Boston">
      <option value="Cambridge">
    </datalist>

This creates a free text input field that also has a drop-down to select predefined choices. Attribution for example and more information: https://www.w3.org/wiki/HTML/Elements/datalist

这将创建一个自由文本输入字段,该字段还有一个用于选择预定义选项的下拉列表。例如归属和更多信息:https: //www.w3.org/wiki/HTML/Elements/datalist