javascript:取消选中所有复选框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15640087/
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
javascript: Unchecking all checkboxes
提问by Boon
I have problem dealing with unchecking all checkboxes. When I click on a toggle all checkbox, it could check all checkboxes. But if I uncheck the toggle all checkbox, nothing happens. All checkboxes are not unchecked. Below is my coding in javascript:
我在处理取消选中所有复选框时遇到问题。当我单击切换所有复选框时,它可以选中所有复选框。但是,如果我取消选中“全部切换”复选框,则什么也不会发生。并非未选中所有复选框。下面是我在 javascript 中的编码:
<script>
var isAllCheck = false;
function togglecheckboxes(cn){
var cbarray = document.getElementsByName(cn);
for(var i = 0; i < cbarray.length; i++){
if( isAllCheck == false ){
cbarray[i].checked = "true";
//alert( "it is false" );
}else{
cbarray[i].removeAttribute("checked");
//alert( "it is true" );
}
}
isAllCheck = !isAllCheck;
}
</script>
I had even tried this coding, but still failed:
我什至尝试过这种编码,但仍然失败:
<script>
var isAllCheck = false;
function togglecheckboxes(cn){
var cbarray = document.getElementsByName(cn);
for(var i = 0; i < cbarray.length; i++){
if( isAllCheck == false ){
cbarray[i].checked = "true";
//alert( "it is false" );
}else{
cbarray[i].checked = "false";
//alert( "it is true" );
}
}
isAllCheck = !isAllCheck;
}
</script>
Below is my PHP coding for your reference:
以下是我的 PHP 编码供您参考:
echo "\t<div class='item'>
<span class='CDTitle'>{$cd['CDTitle']}</span>
<span class='CDYear'>{$cd['CDYear']}</span>
<span class='catDesc'>{$cd['catDesc']}</span>
<span class='CDPrice'>{$cd['CDPrice']}</span>
<span class='chosen'><input type='checkbox' name='cd[]' value='{$cd['CDID']}' title='{$cd['CDPrice']}' /></span>
</div>\n";
Any tips on resolving this problem. Thanks in advance!
有关解决此问题的任何提示。提前致谢!
回答by Jamiec
The problem boils down to how you're setting the checked
property of the checkox. You're assigning a string with "true" where you should be assigning the boolean value true
.
问题归结为您如何设置checked
checkox的属性。您正在分配一个带有“true”的字符串,您应该在其中分配布尔值true
。
So to fix your code is easy:
所以修复你的代码很容易:
if( isAllCheck == false ){
cbarray[i].checked = true;
}else{
cbarray[i].checked = false;
}
or, more succinctly
或者,更简洁地
cbarray[i].checked = !isAllCheck
Live example: http://jsfiddle.net/SEJZP/
现场示例:http: //jsfiddle.net/SEJZP/
回答by Vaishnavi
function uncheckElements()
{
var uncheck=document.getElementsByTagName('input');
for(var i=0;i<uncheck.length;i++)
{
if(uncheck[i].type=='checkbox')
{
uncheck[i].checked=false;
}
}
}
回答by Matthias S
You could also make it a Boolean Variable.
您也可以将其设为布尔变量。
<script>
var isAllCheck = new Boolean(false);
function togglecheckboxes(cn){
var cbarray = document.getElementsByName(cn);
for(var i = 0; i < cbarray.length; i++){
if(isAllCheck){ //and then you could make this shorter.
cbarray[i].checked = true;
//alert( "it is false" );
}else{
cbarray[i].removeAttribute("checked");
//alert( "it is true" );
}
}
isAllCheck = !isAllCheck;
}
</script>
回答by iJade
Try this
试试这个
<script>
var isAllCheck = false;
function togglecheckboxes(master,cn){
var cbarray = document.getElementsByName(cn);
for(var i = 0; i < cbarray.length; i++){
if( isAllCheck == false ){
cbarray[i].checked = true;
//alert( "it is false" );
}else{
cbarray[i].checked = false;
//alert( "it is true" );
}
}
isAllCheck = !isAllCheck;
}
</script>
回答by Mesut Tasci
You can do this with jQuery very easy and more understandable :)
您可以使用 jQuery 非常简单易懂地做到这一点:)
$('#mybutton').click(function(e) {
$('.myCheckboxes').each(function(i,item){
$(item).attr('checked', !$(item).is(':checked'));
});
});