Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

javascript - 我可以将所需属性应用于HTML5中的<select>字段吗?(Can I apply the required attribute to <select> fields in HTML5?)

How can I check if a user has selected something from a <select> field in HTML5?

(如何检查用户是否从HTML5中的<select>字段中选择了某些内容?)

I see <select> doesn't support the new required attribute... do I have to use JavaScript then?

(我看到<select>不支持新的required属性...那么我必须使用JavaScript吗?)

Or is there something I'm missing?

(还是有什么我想念的?)

:/

(:/)

  ask by Matt translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

(必填 :将第一个值设为空 - 必需对空值起作用)

Prerequisites : correct html5 DOCTYPE and a named input field

(先决条件 :正确的html5 DOCTYPE和命名输入字段)

<select name="somename" required>
<option value="">Please select</option>
<option value="one">One</option>
</select>

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

(根据文档 (列表和粗体是我的))

The required attribute is a boolean attribute.

(required属性是布尔属性。)


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);

    (并且显示尺寸为1(没有SIZE = 2或更多 - 如果不需要则省略它);)

  • and if the value of the first option element in the select element's list of options (if any) is the empty string (ie present as value="" ),

    (如果select元素的选项列表中的第一个选项元素的值(如果有的话)是空字符串 (即表示为value="" ),)

  • and that option element's parent node is the select element (and not an optgroup element),

    (并且该选项元素的父节点是select元素(而不是optgroup元素),)

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

(然后该选项是select元素的占位符标签选项。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...