HTML 复选框 - 只允许选中一个复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19877370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 15:12:22  来源:igfitidea点击:

HTML checkbox - allow to check only one checkbox

htmlcheckbox

提问by Tony

I have some checkboxes in each row in my table. Each one checkbox has name='myName'because I want to select only one checkbox in each row. But something I'm missing because I'm able to check all of them:

我的表格中的每一行都有一些复选框。每个复选框都有,name='myName'因为我只想在每一行中选择一个复选框。但是我遗漏了一些东西,因为我能够检查所有这些:

enter image description here

在此处输入图片说明

but I want that result:

但我想要这个结果:

enter image description here

在此处输入图片说明

what am I missing here ?

我在这里错过了什么?

回答by silicakes

The unique name identifier applies to radio buttons:

唯一名称标识符适用于单选按钮:

<input type="radio" />

change your checkboxes to radio and everything should be working

将您的复选框更改为收音机,一切都应该正常工作

回答by Charlie74

Checkboxes, by design, are meant to be toggled on or off. They are not dependent on other checkboxes, so you can turn as many on and off as you wish.

根据设计,复选框旨在打开或关闭。它们不依赖于其他复选框,因此您可以根据需要打开和关闭任意数量的复选框。

Radio buttons, however, are designed to only allow one element of a group to be selected at any time.

然而,单选按钮被设计为只允许在任何时候选择一组元素中的一个元素。

References:

参考:

Checkboxes: MDN Link

复选框: MDN 链接

Radio Buttons: MDN Link

单选按钮: MDN 链接

回答by Bhanu Pratap

 $(function () {
     $('input[type=checkbox]').click(function () {
         var chks = document.getElementById('<%= chkRoleInTransaction.ClientID %>').getElementsByTagName('INPUT');
         for (i = 0; i < chks.length; i++) {
            chks[i].checked = false;
         }
         if (chks.length > 1)
            $(this)[0].checked = true;
     });
 });

回答by user7576086

sapSet = mbo.getThisMboSet()
sapCount = sapSet.count()
saplist = []

if sapCount > 1:
   for i in range(sapCount):`enter code here`
     defaultCheck = sapSet.getMbo(i)
     saplist.append(defaultCheck.getInt("HNADEFACC"))
   defCount = saplist.count(1)
   if defCount > 1:
      errorgroup = " Please Note: you are allowed"
      errorkey = "  only One Default Account"
   if defCount < 1:
      errorgroup = " Please enter "
      errorkey = "  at leat One Default Account"
else:
   mbo.setValue("HNADEFACC",1,MboConstants.NOACCESSCHECK)

回答by j9070749

$('#OvernightOnshore').click(function () {
    if ($('#OvernightOnshore').prop("checked") == true) {
        if ($('#OvernightOffshore').prop("checked") == true) {
            $('#OvernightOffshore').attr('checked', false)
        }
    }
})

$('#OvernightOffshore').click(function () {
    if ($('#OvernightOffshore').prop("checked") == true) {
        if ($('#OvernightOnshore').prop("checked") == true) {
            $('#OvernightOnshore').attr('checked', false);
        }
    }
})

This above code snippet will allow you to use checkboxes over radio buttons, but have the same functionality of radio buttons where you can only have one selected.

上面的代码片段将允许您在单选按钮上使用复选框,但具有与单选按钮相同的功能,您只能选择一个。