我可以将所需的属性应用到 <select> 吗?HTML5 中的字段? [英] Can I apply the required attribute to &lt;select&gt; fields in HTML5?

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

问题描述

如何检查用户是否从 HTML5 的 不支持新的 required 属性...那我必须使用 JavaScript 吗?还是我遗漏了什么?:/

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: 将第一个值设为空 - 需要处理空值

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

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

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.

如果一个选择元素

  • 指定了一个必需的属性,
  • 没有指定多个属性,
  • 并且显示大小为 1(不要有 SIZE=2 或更多 - 如果不需要,请省略它);
  • 如果值中的第一个选项元素选择元素的选项列表(如果any) 是空字符串(即显示为 value=""),
  • 还有那个option 元素的父节点是选择元素(而不是 optgroup元素),

那么那个选项就是选择元素的占位符标签选项.

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

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

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