How to input text in HTML Select Tag?

亡梦爱人 提交于 2019-11-29 02:37:32

HTML solution with "list" attribute:

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

You will have to use javascript to get the additional value. Check this post for some example code:

Jquery dynamically update "other" option in select

Select elements can't contain anything other than option or optgroup elements. Here's a ref to the spec http://www.w3.org/TR/html5/forms.html#the-select-element

You may be better off adding an option for "other" in your dropdown and then using JS to detect for that choice to dynamically show an input (below the dropdown) for a custom value.

Position an input box over the select box and remove the border of the input box. Make the length of the input box shorter than the select box so that the select box may still be used at its end.

Use the oninput event to detect input being entered in the input box. On each keystroke check for a continuing match in the select box. When a match no longer exists there is no further need for the select box.

The server will expect to receive both the text box input, if any, and the select box input, if any, and should use the select value if provided otherwise the input value if provided.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!