Does the select element have the required attribut

2019-01-31 17:46发布

Does the select element have the required attribute?

4条回答
Animai°情兽
2楼-- · 2019-01-31 18:03

Yes you can use required attribute in HTML5. But remember, first value should be empty.

<select required>
<option value="">Please select</option>
<option value="first">First</option>
</select>

Here you get the more example:

http://dev.w3.org/html5/spec-author-view/the-select-element.html#the-select-element

查看更多
3楼-- · 2019-01-31 18:07

Yes it does, but currently it is not supported by any version of all major browsers. This includes Safari, Chrome, Firefox, and IE.

查看更多
forever°为你锁心
4楼-- · 2019-01-31 18:15

It is possible but (just Arif said above) it is important (obviously) that you use the first option without value like:

<form action="#" method="post">
  <div>
    <label for="State">State</label> 
    <select required id="State" name="State">
      <option value="">Choose</option>
      <option value="new">New</option>
      <option value="old">Old</option>
    </select>
  </div>
</form>

You can see more info at: http://www.maxdesign.com.au/2012/11/03/select-required/

查看更多
5楼-- · 2019-01-31 18:25

Yes it has a required attribute, you can use it as follows

<select required>
<option value="" disabled selected>Choose</option>
<option value="first Option">First Option</option>
<option value="Second Option">Second Option</option>
</select>

Reference :

HTML Select required Attribute (W3C)

查看更多
登录 后发表回答