Javascript切换复选框 [英] Javascript toggle checkbox
本文介绍了Javascript切换复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用单个功能切换所有按钮。函数需要切换文档中的所有复选框,因为我的复选框是独立的,不是表单的一部分。
I need to toggle all buttons with a single function. Function needs to toggle all checkboxes in the document as my checkboxes are freestanding and not part of a form.
我目前有这个,但是无法正常工作。我在firefox控制台中收到语法错误:语法错误
。
I currently have this, but it is not working properly. I get syntax error: syntax error
in my firefox console.
checked=false;
function checkedAll() {
var c = new Array();
c = doc.getElementsByTagName('input');
if (checked == false){
checked = true;
}else{
checked = false;
}
for (var i = 0; i < c.length; i++){
if (c[i].type == 'checkbox'){
c[i].checked = checked;
}
}
}
如何修复我的代码?
感谢
推荐答案
首先,而不是 doc
它必须文档
。
Two main items to refactor. First, instead of doc
it must be document
. Second instead of relying on a global just pass in a boolean to determine whether or not to check the checkboxes.
function checkedAll(isChecked) {
var c = document.getElementsByTagName('input');
for (var i = 0; i < c.length; i++){
if (c[i].type == 'checkbox'){
c[i].checked = isChecked;
}
}
}
这篇关于Javascript切换复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文