Mandatory : Have the first value empty - required works on empty values
(必填 :将第一个值设为空 - 必需对空值起作用)
Prerequisites : correct html5 DOCTYPE and a named input field
(先决条件 :正确的html5 DOCTYPE和命名输入字段)
<select name="somename" required>
<option value="">Please select</option>
<option value="one">One</option>
</select>
As per the documentation (the listing and bold is mine)
(根据文档 (列表和粗体是我的))
The required attribute is a boolean attribute.
(required属性是布尔属性。)
When specified, the user will be required to select a value before submitting the form.(指定后,用户需要在提交表单之前选择一个值。)
If a select element
(如果是一个选择元素)
- has a required attribute specified,
(指定了必需属性,)
- does not have a multiple attribute specified,
(没有指定多个属性,)
- and has a display size of 1 (do not have SIZE=2 or more - omit it if not needed);
(并且显示尺寸为1(没有SIZE = 2或更多 - 如果不需要则省略它);)
- and if the value of the first option element in the select element's list of options (if any) is the empty string (ie present as
value=""
),(如果select元素的选项列表中的第一个选项元素的值(如果有的话)是空字符串 (即表示为value=""
),)
- and that option element's parent node is the select element (and not an optgroup element),
(并且该选项元素的父节点是select元素(而不是optgroup元素),)
then that option is the select element's placeholder label option.
(然后该选项是select元素的占位符标签选项。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…