简体   繁体   English

一次切换多个复选框

[英]Toggle multiple Checkbox at once

I have these checkboxs: 我有以下复选框:

'<input type="checkbox" name="checkBox1" value="checked" class= "toggleable">'; 
'<input type="checkbox" name="checkBox2" value="checked" class= "toggleable">'; 
'<input type="checkbox" name="checkBox3" value="checked" class= "toggleable">'; 

Which I can check or uncheck by clinking on them. 我可以通过点击它们来选中或取消选中它们。
Now I am trying to add one more checkbox, which will check or uncheck all previous checkbox at once when clicking. 现在,我试图再添加一个复选框,单击该复选框可以一次选中或取消选中所有先前的复选框。

'<input type="checkbox" name="check_uncheck_all" value="false" id="id_check_uncheck_all">'

What is the JS code to execute when clicking the checkbox to check or uncheck all checkbox "toggleable" at once? 单击复选框一次选中或取消选中所有“可切换”复选框时要执行的JS代码是什么?

Here's an example on how to make this work in pure JavaScript. 这是有关如何在纯JavaScript中实现此功能的示例。

 var checkAll = document.getElementById("id_check_uncheck_all"); checkAll.addEventListener("change", function() { var checked = this.checked; var otherCheckboxes = document.querySelectorAll(".toggleable"); [].forEach.call(otherCheckboxes, function(item) { item.checked = checked; }); }); 
 <label for="id_check_uncheck_all">Select All</label> <input type="checkbox" name="check_uncheck_all" value="false" id="id_check_uncheck_all"> <br/> <input type="checkbox" name="checkBox1" value="checked" class= "toggleable"> <input type="checkbox" name="checkBox2" value="checked" class= "toggleable"> <input type="checkbox" name="checkBox3" value="checked" class= "toggleable"> 

Just verify through JS that if particular checkbox checked. 只需通过JS验证是否已选中特定checkbox It has to check all of them. 它必须检查所有这些。

JS Demo Below: UPDATE 下面的JS演示:更新

Just added id to your inputs. 刚刚在您的输入中添加了id

 function checkAll(x) { if(x.checked == true){ document.getElementById('c1').checked = true; document.getElementById('c2').checked = true; document.getElementById('c3').checked = true; }else{ document.getElementById('c1').checked = false; document.getElementById('c2').checked = false; document.getElementById('c3').checked = false; } } 
 Multiple Check Box Below: <br> <input type="checkbox" name="checkBox1" value="checked" class="toggleable" id="c1"> <input type="checkbox" name="checkBox2" value="checked" class="toggleable" id="c2"> <input type="checkbox" name="checkBox3" value="checked" class="toggleable" id="c3"> </br> Check below to Check them all: </br> <input type="checkbox" name="check_uncheck_all" onchange='checkAll(this);' value="false" id="id_check_uncheck_all"> 

JQuery Demo below: 下面的JQuery演示:

 $('#checkAll').click(function () { $('input:checkbox').prop('checked', this.checked); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <input type="checkbox" id="checkAll" > Check All </br> <input type="checkbox" id="checkItem"> Item 1 <input type="checkbox" id="checkItem"> Item 2 <input type="checkbox" id="checkItem"> Item3 

try it 试试吧

 $(document).ready(function() { $(".toggleable").click(function() { var checkboxes = $(".toggleable"); if($(this).prop("checked")) checkboxes.prop("checked",true); else checkboxes.prop("checked",false); }) }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="checkbox" name="checkBox1" class= "toggleable"> <input type="checkbox" name="checkBox2" class= "toggleable"> <input type="checkbox" name="checkBox3" class= "toggleable"> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM