HTML Form: Select-Option vs Datalist-Option

后端 未结 8 1600
心在旅途
心在旅途 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: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."

提交回复
热议问题