HTML 属性:required

布尔值 required 属性(如果存在)指示用户必须在提交所属表单之前指定输入值。

¥The Boolean required attribute, if present, indicates that the user must specify a value for the input before the owning form can be submitted.

textsearchurltelemailpassworddatemonthweektimedatetime-localnumbercheckboxradiofile<input> 类型以及 <select><textarea> 表单控制元素支持 required 属性。如果出现在任何这些输入类型和元素上,则 :required 伪类将匹配。如果不包含该属性,则 :optional 伪类将匹配。

¥The required attribute is supported by text, search, url, tel, email, password, date, month, week, time, datetime-local, number, checkbox, radio, file, <input> types along with the <select> and <textarea> form control elements. If present on any of these input types and elements, the :required pseudo class will match. If the attribute is not included, the :optional pseudo class will match.

该属性不受 rangecolor 支持或不相关,因为两者都有默认值。hidden 也不支持它,因为不能期望用户填写隐藏的表单。任何按钮类型(包括 image)也不支持它。

¥The attribute is not supported or relevant to range and color, as both have default values. It is also not supported on hidden as it can not be expected that a user to fill out a form that is hidden. Nor is it supported on any of the button types, including image.

注意 colorrange 不支持 required,因为两者始终都有一个值。类型 color 默认为 #000000range 的默认值是 minmax 之间的中点 - 如果未声明,则 minmax 在大多数浏览器中分别默认为 0 和 100。

¥Note color and range don't support required as both always have a value. Type color defaults to #000000. The default for range is the midpoint between min and max — with min and max defaulting to 0 and 100 respectively in most browsers if not declared.

对于同名的 radio 按钮组,如果该组中的单个单选按钮具有 required 属性,则必须选中该组中的单选按钮,尽管它不必是该属性所在的单选按钮 应用。为了改进代码维护,建议在组中的每个同名单选按钮中包含 required 属性,或者不包含 required 属性。

¥In the case of a same named group of radio buttons, if a single radio button in the group has the required attribute, a radio button in that group must be checked, although it doesn't have to be the one on which the attribute is applied. To improve code maintenance, it is recommended to either include the required attribute in every same-named radio button in the group, or else in none.

对于 checkbox 输入类型的相同命名组,仅需要具有 required 属性的复选框。

¥In the case of a same named group of checkbox input types, only the checkboxes with the required attribute are required.

注意:设置 aria-required="true" 告诉屏幕阅读器某个元素(任何元素)是必需的,但与该元素的可选性无关。

¥Note: Setting aria-required="true" tells a screen reader that an element (any element) is required, but has no bearing on the optionality of the element.

属性交互

¥Attribute interactions

由于只读字段不能有值,因此 required 对同时指定了 readonly 属性的输入没有任何影响。

¥Because a read-only field cannot have a value, required does not have any effect on inputs with the readonly attribute also specified.

可用性

¥Usability

当包含 required 属性时,请在控件附近提供可见指示,通知用户需要 <input><select><textarea>。此外,使用 :required 伪类来定位所需的表单控件,并以指示它们是必需的方式对它们进行样式化。这提高了视力正常的用户的可用性。辅助技术应告知用户,根据所需属性,表单控件是强制性的,但添加 aria-required="true" 不会有什么坏处,以防浏览器/屏幕阅读器组合尚不支持 required

¥When including the required attribute, provide a visible indication near the control informing the user that the <input>, <select> or <textarea> is required. In addition, target required form controls with the :required pseudo-class, styling them in a way to indicate they are required. This improves usability for sighted users. Assistive technology should inform the user that the form control is mandatory based on the required attribute, but adding aria-required="true" doesn't hurt, in case the browser / screen reader combination does not support required yet.

约束验证

¥Constraint validation

如果该元素是必需的并且该元素的值为空字符串,则该元素患有 valueMissing 并且该元素将匹配 :invalid 伪类。

¥If the element is required and the element's value is the empty string, then the element is suffering from valueMissing and the element will match the :invalid pseudo class.

无障碍问题

¥Accessibility concerns

向用户提供指示,告知他们需要表单控件。确保消息传递是多方面的,例如通过文本、颜色、标记和属性,以便所有用户了解需求,无论他们有色盲、认知差异还是正在使用屏幕阅读器。

¥Provide an indication to users informing them the form control is required. Ensure the messaging is multi-faceted, such as through text, color, markings, and attribute, so that all users understand the requirements whether they have color blindness, cognitive differences, or are using a screen reader.

示例

¥Example

HTML

html
<form>
  <div class="group">
    <input type="text" />
    <label>Normal</label>
  </div>
  <div class="group">
    <input type="text" required />
    <label>Required</label>
  </div>
  <input type="submit" />
</form>

结果

¥Result

也可以看看