如何检查是否选择了多个选择框
我的表单中有四个选择框。用户必须仅从一个选择框中选择一个值,并且四个值中的哪一个并不重要。鉴于我无法使用 JS (会更容易)并且每个选择框都有自己的名称,我如何检查是否只选择了其中一个?
例如,如果只选择一个就可以了。但如果选择了多个,我想显示错误。
我正在考虑编写几个 if() 语句,但实际上我正在寻找更优雅的解决方案。
我正在使用 PHP。
这是一个表单示例:
<select name="hr_id">
<option value="1">...</option>
</select>
<select name="pr_id">
<option value="1">...</option>
</select>
<select name="dev_id">
<option value="1">...</option>
</select>
<select name="sales_id">
<option value="1">...</option>
</select>
I have four select boxes in a form. The user must select a value only from one select box and it doesn't matter which of the four ones. Given the fact that I can't use JS (would be easier) and that each select box has its own name, how can I check if only one of them is selected?
For example if only one is selected its ok. But if there's more than one selected I want to display an error.
I'm thinking about writing a couple of if() statements, but I'm actually looking for a more elegant solution.
I'm using PHP.
Here's a form sample:
<select name="hr_id">
<option value="1">...</option>
</select>
<select name="pr_id">
<option value="1">...</option>
</select>
<select name="dev_id">
<option value="1">...</option>
</select>
<select name="sales_id">
<option value="1">...</option>
</select>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑:我认为这不够灵活。您还可以执行以下操作:
EDIT: I suppose this isn't exactly flexible enough. You could also do something like:
为什么不使用默认具有此功能的单选按钮?
Why not use radio buttons which have this functionality by default?