[英]how to detect when a user clicks on an option in a drop down menu (select tag)
[英]How do I make the “select one” option not go through when the user clicks submit on the select tag?
在我的表单上,我试图使选择框显示标准的“选择一个”选项,以便用户可以单击它,然后在我已选择的3个选项中进行选择。 我希望表单的这一部分是必需的,同时也不允许默认选项通过。 任何使第一个选项不起作用的帮助将不胜感激! 谢谢!
<option selected="selected">Select one</option>
<option value="air">On the air</option>
<option value="web">On the web</option>
<option value="both">Both</option>
只是改变
<select required>
<option value="" selected>Select one</option>
<option value="air">On the air</option>
<option value="web">On the web</option>
<option value="both">Both</option>
</select>
用户永远不能提交默认选项。
您可以像这样selected
和disabled
“ Select one
选项。
function validate(self) { console.log(self.value); }
<select onchange="validate(this)" required> <option disabled selected>Select one</option> <option value="air">On the air</option> <option value="web">On the web</option> <option value="both">Both</option> </select>
onchange
事件。 selected
标签确保其为默认选项。 disabled
标记确保不能将其选择为实际选项。 required
标记可阻止表单提交。 “选择一个”文本实际上不应该是一个选项,而应该是一个标签
<label>Select One:</label> <select> <option value="air">On the air</option> <option value="web">On the web</option> <option value="both">Both</option> </select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.