使单选按钮“可检查”:HTML

发布于 2024-08-19 06:49:54 字数 45 浏览 4 评论 0原文

如何使单选按钮的操作更像复选框?我需要单选按钮的功能,但需要能够取消选择它。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

Spring初心 2024-08-26 06:49:54

通常这是通过添加另一个选项“无”来完成的。用户不会期望单击选定的单选按钮会取消选择它,因为这不是正常行为。

另一个不再经常使用的选项是“清除”按钮。但我不喜欢这个选项。

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.

吻风 2024-08-26 06:49:54

您可以创建一个“无”选项,或者创建一个 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...

嗼ふ静 2024-08-26 06:49:54

您可以向每个单选按钮添加一个 onClick 函数,如果在选择时单击该单选按钮,该函数将取消选择它(尚未尝试过,但似乎合理)。不过,对于大多数用户来说,这确实是意想不到的行为 - 我认为建议“无”选项的其他答案更好。

编辑:只是为了好玩,我尝试了一下。有用。我的代码非常俗气 - 只是为了快速测试而完成的。

<script>
  var x = false;
</script>

<input type="radio" value="test 1" name="1" onmousedown="if (this.checked) { x = true; }" onclick="if (x) {this.checked = false; x = false; return true;}" />1
<input type="radio" value="test 2" name="1" onmousedown="if (this.checked) { x = true; }" onclick="if (x) {this.checked = false; x = false; return true;}" />2
<input type="radio" value="test 3" name="1" onmousedown="if (this.checked) { x = true; }" onclick="if (x) {this.checked = false; x = false; return true;}" />3

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.

<script>
  var x = false;
</script>

<input type="radio" value="test 1" name="1" onmousedown="if (this.checked) { x = true; }" onclick="if (x) {this.checked = false; x = false; return true;}" />1
<input type="radio" value="test 2" name="1" onmousedown="if (this.checked) { x = true; }" onclick="if (x) {this.checked = false; x = false; return true;}" />2
<input type="radio" value="test 3" name="1" onmousedown="if (this.checked) { x = true; }" onclick="if (x) {this.checked = false; x = false; return true;}" />3
谜兔 2024-08-26 06:49:54

您可以按照其他人的建议使用“无”选项。如果这是不可接受的,您的其他选项是:

  1. 选中具有逻辑的复选框以仅接受一个选择。
  2. 只允许单个选择的列表框。

You can use the "none" option as suggested by others. If that is not acceptable your other options are:

  1. Check boxes with logic to accept only one selection.
  2. A list box that only allows single selections.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文