HTML Form: Select-Option vs Datalist-Option

后端 未结 8 1556
心在旅途
心在旅途 2020-11-28 02:41

I was wondering what the differences are between Select-Option and Datalist-Option. Is there any situation in which it would be better to use one or the other? An example

相关标签:
8条回答
  • 2020-11-28 03:23

    Datalist includes autocomplete and suggestions natively, it can also allow a user to enter a value that is not defined in the suggestions.

    Select only gives you pre-defined options the user has to select from

    0 讨论(0)
  • 2020-11-28 03:25

    To specifically answer a part of your question "Is there any situation in which it would be better to use one or the other?", consider a form with repeating sections. If the repeating section contains many select tags, then the options must be rendered for each select, for every row.

    In such a case, I would consider using datalist with input, because the same datalist can be used for any number of inputs. This could potentially save a large amount of rendering time on the server, and would scale much better to any number of rows.

    0 讨论(0)
  • 2020-11-28 03:27

    Think of it as the difference between a requirement and a suggestion. For the select element, the user is required to select one of the options you've given. For the datalist element, it is suggested that the user select one of the options you've given, but he can actually enter anything he wants in the input.

    Edit 1: So which one you use depends upon your requirements. If the user must enter one of your choices, use the select element. If the use can enter whatever, use the datalist element.

    Edit 2: Found this tidbit in the HTML Living Standard: "Each option element that is a descendant of the datalist element...represents a suggestion."

    0 讨论(0)
  • 2020-11-28 03:27

    Its similar to select, But datalist has additional functionalities like auto suggest. You can even type and see suggestions as and when you type.

    User will also be able to write items which is not there in list.

    0 讨论(0)
  • 2020-11-28 03:31

    Data List is a new HTML tag in HTML5 supported browsers. It renders as a text box with some list of options. For Example for Gender Text box it will give you options as Male Female when you type 'M' or 'F' in Text Box.

    <input list="Gender">
    <datalist id="Gender">
      <option value="Male">
      <option value="Female> 
    </datalist>
    
    0 讨论(0)
  • 2020-11-28 03:35

    There is another important difference between select and datalist. Here comes the browser support factor.

    select is widely supported by browsers compared to datalist. Please take a look at this page for complete browser support of datalist--

    Datalist browser support

    Where as select is supported in effectively all browsers (since IE6+, Firefox 2+, Chrome 1+ etc)

    0 讨论(0)
提交回复
热议问题