我可以将必需的属性应用于
如何检查用户是否从 HTML 中的
我看到
How can I check if a user has selected something from a <select>
field in HTML?
I see <select>
doesn't support the new required
attribute... do I have to use JavaScript then? Or is there something I’m missing? :/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
强制:将第一个值设为空 - 要求对空值起作用
先决条件:正确的 html5 DOCTYPE 和命名输入
字段w3.org/html5/spec-author-view/the-select-element.html#the-select-element" rel="noreferrer">文档(列表和粗体是我的)
Mandatory: Have the first value empty - required works on empty values
Prerequisites: correct html5 DOCTYPE and a named input field
As per the documentation (the listing and bold is mine)
根据规范,
哪个浏览器不支持这一点?
(当然,无论如何,您都必须在服务器上进行验证,因为您无法保证用户将启用 JavaScript。)
The
<select>
element does support therequired
attribute, as per the spec:Which browser doesn’t honour this?
(Of course, you have to validate on the server anyway, as you can’t guarantee that users will have JavaScript enabled.)
是的,它正在工作:
您必须将第一个选项保留为空。
Yes, it's working:
you have to keep first option blank.
您可以使用选项元素的
selected
属性来默认选择一个选项。您可以使用 select 元素的required
属性来确保用户选择某些内容。在 Javascript 中,您可以检查
selectedIndex
属性来获取所选选项的索引,也可以检查value
属性来获取所选选项的值。根据 HTML5 规范,
selectedIndex
“返回第一个选定项目的索引(如果有),如果没有选定项目,则返回 -1。value
”返回值第一个选定项目的位置(如果有),或者如果没有选定项目,则为空字符串。”因此,如果 selectedIndex = -1,则您知道他们没有选择任何内容。You can use the
selected
attribute for the option element to select a choice by default. You can use therequired
attribute for the select element to ensure that the user selects something.In Javascript, you can check the
selectedIndex
property to get the index of the selected option, or you can check thevalue
property to get the value of the selected option.According to the HTML5 spec,
selectedIndex
"returns the index of the first selected item, if any, or −1 if there is no selected item. Andvalue
"returns the value of the first selected item, if any, or the empty string if there is no selected item." So if selectedIndex = -1, then you know they haven't selected anything.您需要将
option
的value
属性设置为空字符串:select
将返回所选的value
当用户在表单
上按submit
时,向服务器发送选项
。空的值
与空的文本
输入相同 ->引发required
消息。w3school
You need to set the
value
attribute ofoption
to the empty string:select
will return thevalue
of the selectedoption
to the server when the user pressessubmit
on theform
. An emptyvalue
is the same as an emptytext
input -> raising therequired
message.w3schools
试试这个,这会起作用,我已经尝试过了,这起作用了。
try this, this gonna work, I have tried this and this works.
将选择框第一项的值设置为空白。
因此,当您每次发布表格时,您都会得到空白值,并且使用这种方式您会知道用户没有从下拉列表中选择任何内容。
Make the value of first item of selection box to blank.
So when every you post the FORM you get blank value and using this way you would know that user hasn't selected anything from dropdown.
首先,您必须在第一个选项中分配空白值。
即选择此处。仅需要即可工作。
first you have to assign blank value in first option.
i.e. Select here.than only required will work.
如果第一个选项的值为空,则工作完全正常。说明 :HTML5 将在按钮提交时读取空值。如果不为空(值属性),则假定所选值不为空,因此验证将起作用,即通过检查选项标记中是否有数据。因此它不会产生验证方法。然而,我想另一面会变得很清楚,如果 value 属性设置为 null 即 (value = "" ),HTML5 将在第一个或默认选择的选项上检测到空值,从而发出验证消息。谢谢你的询问。很高兴能帮助你。很高兴知道我是否做到了。
Works perfectly fine if the first option's value is null. Explanation : The HTML5 will read a null value on button submit. If not null (value attribute), the selected value is assumed not to be null hence the validation would have worked i.e by checking if there's been data in the option tag. Therefore it will not produce the validation method. However, i guess the other side becomes clear, if the value attribute is set to null ie (value = "" ), HTML5 will detect an empty value on the first or rather the default selected option thus giving out the validation message. Thanks for asking. Happy to help. Glad to know if i did.
在 html5 中,您可以使用完整表达式:
我不知道为什么短表达式不起作用,但尝试一下这个。
它会解决。
In html5 you can do using the full expression:
I don't know why the short expression doesn't work, but try this one.
It will solve.
试试这个
Try this
您也可以使用 JQuery 动态执行此操作
Set required
Remove required
You can do it also dynamically with JQuery
Set required
Remove required