简体   繁体   English

如何使用javascript选中1复选框取消选中另一复选框,反之亦然?

[英]How to use javascript to have checking 1 checkbox uncheck the other and vice versa?

The reason not to use radio buttons here is because I want the option to have all checkboxes unchecked, and there are other behaviors linked to checking and unchecking. 之所以不使用单选按钮,是因为我希望该选项不选中所有复选框,并且还有其他行为与选中和取消选中关联。

When the 1st box is checked, all radio buttons in the row are selected. 选中第一个框时,将选择该行中的所有单选按钮。 (see here How to use javascript to select a row of radio buttons when a checkbox is selected ) (请参阅此处,选中复选框后如何使用javascript选择单选按钮行

When the 1st button is unchecked, all the radios in that row are deselected. 如果未选中第一个按钮,则取消选择该行中的所有无线电。

Whenever a checkbox is checked, the other checkbox should automatically be deselected. 无论何时选中一个复选框,都应自动取消选择另一个复选框。

I'm thinking maybe I could do this by css class. 我在想也许我可以在CSS类中做到这一点。 So, whenever one checkbox in the class is checked, the others are automatically unchecked. 因此,每当选中该类中的一个复选框时,其他复选框都会自动取消选中。

I'm imagining code like this: 我在想像这样的代码:

function uncheckOther(row_id) {
        var row = document.getElementById(row_id)
    var theClassName = row.className;
    var classGroup = document.getElementsByClassName(theClassname);
    for(var i=0; i<classGroup.length; i++) {
        if(classGroup[i].id != row_id) {
            classGroup[i].radio = unchecked;
        }
    }
}

How would I do this? 我该怎么做? Here's the example HTML with id's and classes in the tr elements, and checkboxes in the 1st child td elements: 这是示例HTML,其中tr元素中包含id和class,而第一个子td元素中包含复选框:

<form name="form3" action="testfile4.php" method="get">
<table border="1"><thead>
    <tr><th>Select entire row</th><th>item_code</th><th>page</th><th>usd_dn</th></tr>
</thead>
<tbody>
    <tr id="534" class="15838">
        <td ><input type="checkbox" onclick="select_row(534);"></td>        <td>15838         <input type="radio" name="15838|item_code" value="534" /></td>
        <td>284<input type="radio" name="15838|page" value="534" /></td>
        <td>$73.00<input type="radio" name="15838|usd_dn" value="534" /></td>
    </tr>
    <tr id="535" class="15838">
        <td ><input type="checkbox" onclick="select_row(535);"></td>        <td>15838         <input type="radio" name="15838|item_code" value="535" /></td>
        <td>299
          <input type="radio" name="15838|page" value="535" /></td>
        <td>$73.00<input type="radio" name="15838|usd_dn" value="535" /></td>
    </tr>
    <tr id="565">
        <td ><input type="checkbox" onclick="select_row(565);"></td>        <td>1611          <input type="radio" name="1611|item_code" value="565" /></td>
        <td>66<input type="radio" name="1611|page" value="565" /></td>
        <td>$3,350.00
          <input type="radio" name="1611|usd_dn" value="565" /></td>
    </tr>
    <tr id="566">
        <td ><input type="checkbox" onclick="select_row(566);"></td>        <td>1611          <input type="radio" name="1611|item_code" value="566" /></td>
        <td>66<input type="radio" name="1611|page" value="566" /></td>
        <td>$3,225.00
          <input type="radio" name="1611|usd_dn" value="566" /></td>
    </tr>
    </tbody>
    </table>
    </form>

I'm ok with jquery answers but prefer pure javascript at the moment. 我可以使用jquery答案,但目前更喜欢纯JavaScript。

There are plenty of reasons to use checkboxes instead of radio buttons... 有很多理由使用复选框而不是单选按钮...

if ($('#myDivID').attr('checked'))

This will tell you if it's checked. 这将告诉您是否已检查。 To loop through all, and keep only the current checked one checked, you could do something like this in the $(document).ready() function: 要遍历所有内容并仅选中当前选中的内容,可以在$(document).ready()函数中执行以下操作:

$('input[type="checkbox"]').click(function () {
    $('input[type="checkbox"]').attr('checked', false);
    $(this).attr('checked', true);
});

Any checkbox checked will STAY checked. 选中的所有复选框都会保持选中状态。 The rest will be unchecked. 其余的将不被选中。

The native way would be to use the checked property that's on the checkbox dom object: 本机方式是使用复选框dom对象上的checked属性:

document.getElementById("checkboxId").checked = false;

Or, to toggle: 或者,切换:

var cb = document.getElementById("checkboxId");
cb.checked = !cb.checked;

Here's the documentation for the checkbox dom object, if you're trying to not use jQuery. 如果您尝试不使用jQuery,则这里是复选框dom对象的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果取消选中所有子复选框,如何取消选中父复选框,反之亦然? - how to uncheck parent checkbox if all children checkbox is uncheck and vice versa? Javascript复选框取消选中其他复选框,反之亦然 - Javascript check box to uncheck other boxes and vice versa jQuery逻辑问题。 如果选中了其他复选框,则需要取消选中它,反之亦然 - Problem with jQuery logic. Need to uncheck one checkbox if other is checked, vice versa 如何在django中使用javascript变量,反之亦然? - How to use javascript variables with django or vice versa? 如何使用删除图标在输出 div 中显示子类别复选框标签并在取消选中时将其删除,反之亦然 - How to show sub category checkbox label in output div with remove icon and remove it on uncheck and vice versa 选中复选框后,取消选中页面上的其他复选框 - Uncheck other checkboxes on page when checking a checkbox 如果已经选中了其他复选框,如何使用javascript自动取消选中复选框? - How to automatically uncheck a checkbox with javascript if other is already checked? 检查数组的和是否大于最大数,反之亦然 - checking if sum of array is greater than max number and vice versa javascript 如何取消选中纯 JavaScript 中的复选框? - How to uncheck a checkbox in pure JavaScript? 如何在处理程序中使用 useEffect,反之亦然? - How to use useEffect inside a handler or vice versa?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM