使单选按钮“可检查”:HTML
如何使单选按钮的操作更像复选框?我需要单选按钮的功能,但需要能够取消选择它。
How do I make radio buttons operate more like a check box? I need the function of a radio button, but it needs to be able to be de-selected.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通常这是通过添加另一个选项“无”来完成的。用户不会期望单击选定的单选按钮会取消选择它,因为这不是正常行为。
另一个不再经常使用的选项是“清除”按钮。但我不喜欢这个选项。
Usually this is accomplished by adding another option, "None". Users would not expect that clicking a selected radio button will deselect it since that's not normal behavior.
Another option, not often used anymore, is a "clear" button. I don't like this option though.
您可以创建一个“无”选项,或者创建一个 JavaScript 按钮来取消选择每个单选按钮。我想知道如果单选按钮已经被选中,它的 onclick 事件是否会做出反应......
You could make a "none" option, or create a javascript button to unselect every radio button. I wonder if the onclick event of a radio button react if it is already selected...
您可以向每个单选按钮添加一个 onClick 函数,如果在选择时单击该单选按钮,该函数将取消选择它(尚未尝试过,但似乎合理)。不过,对于大多数用户来说,这确实是意想不到的行为 - 我认为建议“无”选项的其他答案更好。
编辑:只是为了好玩,我尝试了一下。有用。我的代码非常俗气 - 只是为了快速测试而完成的。
You could add an onClick function to each radio button that would de-select it if it was clicked while selected (haven't tried it, but it seems reasonable). This is really unexpected behavior for most users though - I think the other answers suggesting a 'None' option are better.
EDIT: Just for fun, I tried this out. It works. My code is pretty cheesy - done just for a quickie test.
您可以按照其他人的建议使用“无”选项。如果这是不可接受的,您的其他选项是:
You can use the "none" option as suggested by others. If that is not acceptable your other options are: