我在处理取消选中所有复选框时遇到问题。当我单击切换所有复选框时,它可以选中所有复选框。但是,如果我取消选中全部切换复选框,则不会发生任何事情。所有复选框都未选中。以下是我在 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>
我什至尝试过这种编码,但仍然失败:
<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>
以下是我的 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";
有关解决此问题的任何提示。提前致谢!