Can I apply the required attribute to <select> fields in HTML5?

前端 未结 13 2384
不知归路
不知归路 2020-11-22 16:20

How can I check if a user has selected something from a doesn\'t support the new

13条回答
  •  没有蜡笔的小新
    2020-11-22 16:44

    Mandatory: Have the first value empty - required works on empty values

    Prerequisites: correct html5 DOCTYPE and a named input field

    
    

    As per the documentation (the listing and bold is mine)

    The required attribute is a boolean attribute.
    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);
    • and if the value of the first option element in the select element's list of options (if any) is the empty string (i.e. present as value=""),
    • and that option element's parent node is the select element (and not an optgroup element),

    then that option is the select element's placeholder label option.

提交回复
热议问题