HTML5 - 选择多个必填复选框
我在这里写了一些代码: http://jsfiddle.net/anhtran/kXsj9/8/
用户必须在组中选择至少 1 个选项。但这使我必须单击所有这些才能提交表单。没有javascript,如何解决这个问题?
感谢您的帮助:)
I've writen some code here: http://jsfiddle.net/anhtran/kXsj9/8/
Users have to select at least 1 option on the group. But it makes me must click all of them to submit the form. How to do this issue without javascript?
Thanks for any help :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为这个 html5 属性只是应该定义哪些字段是必需的。
你不能用逻辑说“至少需要一个”。
您需要添加自定义 JavaScript 才能使其正常工作(和/或在服务器端进行验证)。
希望这有帮助...
I think this html5 attribute is only supposed to define which fields are required.
You cant put logic in to say "at least one is required".
You will need to add custom javascript for this to work (and/or have validation on the server side).
hope this helps...
理想的答案是使用 HTML5 和
required
属性作为select
元素的一部分,如下所示:是的,我知道它们不是复选框,但最终功能是正是您想要的。遗憾的是,IE 9 和 Safari 5 目前都不支持
required
属性。然而 Chrome 13 和 FF 5 可以。 (在Win 7上测试)The ideal answer would be to use HTML5 and the
required
attribute as part of aselect
element, like so:Yes, I know they are not checkboxes, but the end functionality is exactly what you want. Sadly, neither IE 9 nor Safari 5 currently have support for the
required
attribute. Chrome 13 and FF 5, however, do. (Tested on Win 7)我认为使用 CSS 可以部分完成您所要做的事情。不使用 required 属性,而是在未选择任何内容的情况下隐藏提交按钮。
您可以删除
required
属性并使用与此类似的 CSS:但是,该特定 CSS 不适用于我的 Google Chrome 版本。我在此处提出了一个问题< /a>.不过,它在我的 FF 3.6 上似乎运行良好。
I thought it'd be possible, to do in part, what you were after using CSS. Not using the required attribute but to instead hide the submit button if nothing was selected.
You'd get rid of the
required
attributes and use CSS similar to this:However, that particular CSS is not working on my version of Google Chrome. I've made a question regarding it here. It seems to be working fine on my FF 3.6 though.
如果没有 JavaScript,你就无法做到这一点。
您可以做的就是选择一个默认选项并将其设置为选定状态。
但它不能保证您在提交表单时会选中复选框。
You can't do this without javascript.
What you can do is select a default option and set it as selected.
But it can't assure you that a checkbox is selected when the form is submitted.