简体   繁体   English

如何在选择第一个单选输入时选择所有复选框,并在选择第二个单选输入时取消选中所有复选框? (JavaScript)的

[英]How do I Select All Checkboxes When First Radio Input Selected & Uncheck All Checkboxes when Second radio input selected? (Javascript)

how do I select all checkboxes when the first radio input is selected & uncheck all checkboxes when the second radio input is selected? 如何在选择第一个单选输入时选择所有复选框,并在选择第二个单选输入时取消选中所有复选框? (Javascript)? (JavaScript)的?

I've looked into previous similar questions on here, but they all seem to be about using checkboxes rather than radio buttons, or there aren't ways to unselect. 我已经在这里研究过以前的类似问题,但是它们似乎都与使用复选框而不是单选按钮有关,或者没有取消选择的方法。

  <input type="radio" class="permission" name="permission" value="select" /> Select All<br>
  <input type="radio" class="permission" name="permission" value="deselect" /> Deselect All<br>

   <input type="checkbox" class="custom-selector" /> Checkbox  1
   <input type="checkbox" class="custom-selector" /> Checkbox  2
   <input type="checkbox" class="custom-selector" /> Checkbox  3

Get a list of HTML nodes by class using document.querySelectorAll() . 使用document.querySelectorAll()按类获取HTML节点列表。 Iterate the radios list using NodeList.forEach() , and add a change event listener to each radio button. 迭代radios使用列表NodeList.forEach()添加一个change事件监听到每个单选按钮。 Whenever the listener is called, iterate the checkboxes array with NodeList.forEach() , and update the checked value of each element: 每当调用侦听器时,请使用NodeList.forEach()迭代checkboxes数组,并更新每个元素的checked值:

 var checkboxes = document.querySelectorAll('.custom-selector'); var radios = document.querySelectorAll('.permission'); radios.forEach(function(el) { el.addEventListener('change', function(e) { var checked = el.value === 'select'; checkboxes.forEach(function(el) { el.checked = checked; }); }); }); 
 <input type="radio" class="permission" name="permission" value="select" /> Select All<br> <input type="radio" class="permission" name="permission" value="deselect" /> Deselect All<br> <input type="checkbox" class="custom-selector" /> Checkbox 1 <input type="checkbox" class="custom-selector" /> Checkbox 2 <input type="checkbox" class="custom-selector" /> Checkbox 3 

You can do it using JQuery. 您可以使用JQuery做到这一点。

$('.permission').click(function() {
  $('.custom-selector').prop('checked', $(this).val() == 'select');
});

See below Code Snippet 参见下面的代码片段

 $('.permission').click(function() { $('.custom-selector').prop('checked', $(this).val() == 'select'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="radio" class="permission" name="permission" value="select" /> Select All<br> <input type="radio" class="permission" name="permission" value="deselect" /> Deselect All<br> <input type="checkbox" class="custom-selector" /> Checkbox 1 <input type="checkbox" class="custom-selector" /> Checkbox 2 <input type="checkbox" class="custom-selector" /> Checkbox 3 

 function handleChange(e) { if (e.target.value == "select") { handleCheckUncheck(true); } if (e.target.value == "deselect") { handleCheckUncheck(false); } } function handleCheckUncheck(value) { document.querySelectorAll("input[type='checkbox']").forEach(function(val) { val.checked = value; }) } document.querySelectorAll("input[type='radio']").forEach(function(val) { val.addEventListener("change", handleChange); }) 
  <input type="radio" class="permission" name="permission" value="select" /> Select All<br> <input type="radio" class="permission" name="permission" value="deselect" /> Deselect All<br> <input type="checkbox" class="custom-selector" /> Checkbox 1 <input type="checkbox" class="custom-selector" /> Checkbox 2 <input type="checkbox" class="custom-selector" /> Checkbox 3 

You can do it in a simple way without using jQuery. 您无需使用jQuery,就可以通过简单的方式进行操作。

Add a checkbox whose status will be set for others. 添加一个复选框,其状态将设置为其他复选框。

<label><input type="checkbox" onClick="toggle(this)" /> Select All</label>`

And use this javascript function that will check other checkboxes with the 'custom-selector' class: 并使用此javascript函数,将使用“ custom-selector”类选中其他复选框:

function toggle(source) {
    checkboxes = document.getElementsByName('custom-selector[]');
    for(var i=0, n=checkboxes.length;i<n;i++) {
        checkboxes[i].checked = source.checked;
    }
}

If someone checks the "Select All" checkbox, all others will also be selected. 如果某人选中“全选”复选框,则所有其他人也会被选中。 If it deselects, the effect will be the same. 如果取消选择,效果将相同。

Regards! 问候!

我同意@OriDrori的回答,您不需要加载jQuery即可完成如此简单的任务。

暂无
暂无

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

相关问题 如何在选中单选按钮时取消选中复选框,以及在使用Javascript选中复选框时如何取消选中单选按钮 - How to uncheck checkbox when radio button is checked and uncheck radio button when checkboxes are checked using Javascript 当选中“所有”复选框时,禁用所有复选框 - Disable All Checkboxes When 'All' CheckBoxes Is Selected Javascript:当选中/选中分配给它的单选按钮时,如何使输入/文本字段为必需字段? - Javascript: How do I make an input/text field mandatory(required) when the radio button assigned to it is checked/selected? 当所有复选框均为false时,选中单选按钮 - check radio button when all checkboxes are false 使用收音机和复选框进行JavaScript“全部检查” - JavaScript “Check All” with Radio and Checkboxes 当我选择单选按钮时,将选择所有单选按钮。 我只希望选择一个单选按钮 - when i select radio button then all radio button is selected. i want only one radio button should be selected 当我关闭模态时如何取消选中所有复选框? - How to uncheck all checkboxes when i close a modal? 选择单选按钮时如何禁用多个复选框? - how to disable multiple checkboxes when i select radio button? Javascript取消选中所有复选框 - Javascript uncheck all checkboxes with <input type=“checkbox” name=“QuoteRequiredServices[]” syntax 输入类型单选数据值在尝试获取所有选定输入时仅显示一个 - Input type radio data value only shows one when trying to get all selected input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM