Javascript 将 <select> 字段设为必需?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14353412/
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
Make the <select> Field Required?
提问by Joe Bobby
Possible Duplicate:
Can I apply the required attribute to <select> fields in HTML5?
I'm using the html5 contact form from herebut I'm having issues trying to make the <select>field required. Everything else works fine, just having trouble with this and the dev hasnt responded when I asked about the field. this is what I have
我正在使用此处的 html5 联系表单,但在尝试使该<select>字段成为必需字段时遇到问题。其他一切正常,只是遇到了问题,当我询问该领域时,开发人员没有回应。这就是我所拥有的
<select name="package" id="package" title="Please choose a package" class="required">
<option value="">------------------</option>
<option value="basic">Basic</option>
<option value="plus">Plus</option>
<option value="premium">Premium</option>
</select>
What am I missing to get this working correctly?
我缺少什么才能正常工作?
回答by Brian Hannay
I believe that the top answer to this questionis what you're looking for. To quote,
我相信这个问题的最佳答案是您正在寻找的。报价,
This works for me - Have the first value empty - required works on empty values.
<select required> <option value="">Please select</option> <option value="one">One</option> </select>
这对我有用 - 第一个值为空 - 需要对空值起作用。
<select required> <option value="">Please select</option> <option value="one">One</option> </select>
回答by Luka
If you have a better browser, you can try this (like in thisthread):
如果你有更好的浏览器,你可以试试这个(比如在this线程中):
<select required>
<option value=""></option>
<option value="basic">Basic</option>
<option value="plus">Plus</option>
<option value="premium">Premium</option>
</select>
, but still you should use JavaScript because not a lot of people have browsers that support HTML5 requiredattribute. It's not supported by IE and Safari.
,但您仍然应该使用 JavaScript,因为没有多少人拥有支持 HTML5required属性的浏览器。IE 和 Safari 不支持它。
回答by Jai
check this: http://www.w3.org/TR/html-markup/select.html
检查这个:http: //www.w3.org/TR/html-markup/select.html
global attributes Any attributes permitted globally.
全局属性 全局允许的任何属性。
name = string
The name part of the name/value pair associated with this element for the purposes of form submission.
disabled = "disabled" or "" (empty string) or empty
Specifies that the element represents a disabled control.
form = ID reference NEW
The value of the id attribute on the form with which to associate the element.
size = positive integer
The number of options to show to the user.
multiple = "multiple" or "" (empty string) or empty
If present, indicates that its select element represents a control for selecting zero or more options from a list of options. If not present, indicates that its select element represents a control for selecting a single option from a list of options.
autofocus = "autofocus" or "" (empty string) or empty
Specifies that the element represents a control to which a UA is meant to give focus as soon as the document is loaded.
required = "required" or ""(empty string) or emptySpecifies that the element is a required part of form submission.
名称 = 字符串
用于表单提交的与此元素关联的名称/值对的名称部分。
disabled = "disabled" 或 "" (空字符串) 或空
指定元素表示禁用的控件。
表格 = ID 参考 NEW
表单上与元素关联的 id 属性的值。
大小 = 正整数
显示给用户的选项数。
multiple = "multiple" 或 ""(空字符串)或空
如果存在,则表示它的 select 元素表示用于从选项列表中选择零个或多个选项的控件。如果不存在,则表示它的 select 元素表示用于从选项列表中选择单个选项的控件。
autofocus = "autofocus" 或 ""(空字符串)或空
指定该元素表示一个控件,UA 旨在在文档加载后立即为其提供焦点。
required = "required" or ""(空字符串)或空指定元素是表单提交的必需部分。
so you require a required = "required"attribute to your <select>
所以你需要一个required = "required"属性<select>

