HTML 表单:Select-Option 与 Datalist-Option [英] HTML Form: Select-Option vs Datalist-Option

查看:27
本文介绍了HTML 表单:Select-Option 与 Datalist-Option的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 Select-Option 和 Datalist-Option 之间有什么区别.在任何情况下,使用其中一种更好吗?每个示例如下:

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 of each follows:

选择选项

<select name="browser">
<option value="firefox">Firefox</option>
<option value="ie">IE</option>
<option value="chrome">Chrome</option>
<option value="opera">Opera</option>
<option value="safari">Safari</option>
</select>

数据列表选项

<input type="text" list="browsers">
<datalist id="browsers">
  <option value="Firefox">
  <option value="IE">
  <option value="Chrome">
  <option value="Opera">
  <option value="Safari">
</datalist>

推荐答案

将其视为需求和建议之间的区别.对于 select 元素,用户需要选择您提供的选项之一.对于 datalist 元素,建议用户选择您提供的选项之一,但他实际上可以在输入中输入他想要的任何内容.

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.

编辑 1:所以您使用哪一个取决于您的要求.如果用户必须输入您的选择之一,请使用 select 元素.如果用户可以输入任何内容,请使用 datalist 元素.

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.

编辑 2:在 HTML 生活标准:作为 datalist 元素的后代的每个选项元素......代表一个建议."

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

这篇关于HTML 表单:Select-Option 与 Datalist-Option的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆