How to input text in HTML Select Tag?

前端 未结 4 765
走了就别回头了
走了就别回头了 2020-12-16 06:02

I want to make input option in select tag so user can choose between options or insert different value.
Is it possible?


                        
    
提交评论

  • 2020-12-16 06:44

    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.

    0 讨论(0)
  • 2020-12-16 06:47

    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.

    0 讨论(0)
  • 2020-12-16 06:55

    HTML solution with "list" attribute:

    <input type="text" name="city" list="citynames">
    <datalist id="citynames">
      <option value="Boston">
      <option value="Cambridge">
    </datalist>
    
    0 讨论(0)
  • 提交回复
    热议问题