Html select 元素是否具有 required 属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8287353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does the select element have the required attribute?
提问by Codler
Does the select element have the required attribute?
select 元素是否具有 required 属性?
回答by Ariful Islam
Yes you can use required attribute in HTML5. But remember, first value should be empty.
是的,您可以在 HTML5 中使用 required 属性。但请记住,第一个值应该为空。
<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
http://dev.w3.org/html5/spec-author-view/the-select-element.html#the-select-element
回答by Abubakar Ahmed Ruma
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 :
参考 :
回答by kamau wairegi
You can do this way to make it look better
你可以这样做,让它看起来更好
<select required>
<option hidden="" disabled="disabled" selected="selected" value="">Select subject</option>
<option value="first Option">First Option</option>
<option value="Second Option">Second Option</option>
</select>
回答by Idris Armstrong
Yes it does, but currently it is not supported by any version of all major browsers. This includes Safari, Chrome, Firefox, and IE.
是的,但目前所有主要浏览器的任何版本都不支持它。这包括 Safari、Chrome、Firefox 和 IE。
回答by juanmjimenezs
It is possible but (just Arif said above) it is important (obviously) that you use the first option without value like:
这是可能的,但(只是 Arif 上面说的)很重要(显然)你使用第一个没有价值的选项,比如:
<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/
您可以在以下位置查看更多信息:http: //www.maxdesign.com.au/2012/11/03/select-required/