我可以将必需的属性应用于< select> HTML5中的字段? [英] Can I apply the required attribute to <select> fields in HTML5?

查看:114
本文介绍了我可以将必需的属性应用于< select> HTML5中的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我看到< select> 不支持新的所需的属性...我必须使用JavaScript然后?还是有什么我想念的? :::第一个值为空 - 必需的值为空值

>

先决条件:正确的html5 DOCTYPE和一个指定的输入字段

 < select name =somenamerequired> 
< option value =>请选择< / option>
< option value =one>一个< / option>
< / select>

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


必需属性是布尔
属性。

指定后,用户
将需要选择一个值
在提交表单之前。



如果选择元素




  • 具有必需属性指定

  • 没有指定multiple属性,
  • 并且显示大小为1(不具有SIZE = 2或更多 - 省略它如果不需要);

  • 如果
    中第一个选项元素的值
    选择元素的选项列表(如果
    any)是空字符串
    (即 value =),

  • 选项元素的父节点是
    选择元素(而不是optgroup
    元素),



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



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

I see <select> doesn't support the new required attribute... do I have to use JavaScript then? Or is there something I’m missing? :/

解决方案

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

Prerequisites: correct html5 DOCTYPE and a named input field

<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.
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.

这篇关于我可以将必需的属性应用于&lt; select&gt; HTML5中的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆