Javascript 通过开发者工具检查页面中的所有复选框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14245769/
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
Check all Checkboxes in Page via Developer Tools
提问by Ignacio Correia
I have a loop that creates 20 check-boxesin the same page (it creates different forms). I want via chrome developer toolsto run a JavaScript without the use of any library that CHECKall check-boxesat the same time.
我有一个循环,它在同一页面中创建了 20 个复选框(它创建了不同的表单)。我希望通过chrome 开发人员工具运行 JavaScript,而不使用任何同时检查所有复选框的库。
This is as far as I got:
这是我得到的:
function() {
var aa= document.getElementsByTagName("input");
for (var i =0; i < aa.length; i++){
aa.elements[i].checked = checked;
}
}
PS: I have searched and found a lot of Questions in Stack-Overflow but none worked for me, I'll be glad if someone could find me the correct answer.
PS:我在 Stack-Overflow 中搜索并发现了很多问题,但没有一个对我有用,如果有人能找到我的正确答案,我会很高兴。
回答by Musa
(function() {
var aa= document.getElementsByTagName("input");
for (var i =0; i < aa.length; i++){
if (aa[i].type == 'checkbox')
aa[i].checked = true;
}
})()
With up to date browsers can use document.querySelectorAll
使用最新的浏览器可以使用document.querySelectorAll
(function() {
var aa = document.querySelectorAll("input[type=checkbox]");
for (var i = 0; i < aa.length; i++){
aa[i].checked = true;
}
})()
回答by Vlad Bezden
From Console Dev Tools (F12) you can use query selector as you use in javascript or jQuery code.
在控制台开发工具 (F12) 中,您可以像在 javascript 或 jQuery 代码中一样使用查询选择器。
'$$' - means select all items. If you use '$' instead you will get only first item.
'$$' - 表示选择所有项目。如果您使用 '$' 代替,您将只获得第一项。
So in order to select all checkboxes you can do following
因此,为了选择所有复选框,您可以执行以下操作
$$('input').map(i => i.checked = true)
$$('input').map(i => i.checked = true)
or
或者
$$('input[type="checkbox"').map(i => i.checked = true)
$$('input[type="checkbox"').map(i => i.checked = true)
回答by apsillers
You have it nearly correct. Just use
你说的几乎是正确的。只需使用
aa[i].checked = "checked";
inside the loop.
循环内。
Namely, you need to make sure that:
也就是说,您需要确保:
"checked"is a string, not a variable identifier, and- you index directly on
aa, notaa.elements, which does not exist
"checked"是字符串,而不是变量标识符,并且- 你直接索引
aa, notaa.elements, 不存在
回答by user423430
If you're here for the quick one-liner:
如果您来这里是为了快速单线:
var aa = document.getElementsByTagName("input"); for (var i = 0; i < aa.length; i++) aa[i].checked = true;
回答by jimbo
Try setAttribute.
试试setAttribute。
(function() {
var aa = document.getElementsByTagName("input");
for (var i =0; i < aa.length; i++){
aa.elements[i].setAttribute('checked', 'checked');
}
})();
Edit: added parens to execute the function immediately.
编辑:添加括号以立即执行该功能。
回答by Robin Drexler
Try this :)
尝试这个 :)
(function () {
var checkboxes = document.querySelectorAll('input[type=checkbox]');
//convert nodelist to array
checkboxes = Array.prototype.slice.call(checkboxes);
checkboxes.forEach(function (checkbox) {
console.log(checkbox);
checkbox.setAttribute('checked', true);
});
})()
回答by Prem Kumar Maurya
Javascript function to toggle (check/uncheck) all checkbox.
用于切换(选中/取消选中)所有复选框的 Javascript 函数。
function checkAll(bx)
{
var cbs = document.getElementsByTagName('input');
for(var i=0; i < cbs.length; i++)
{
if(cbs[i].type == 'checkbox')
{
cbs[i].checked = bx.checked;
}
}
}
If you want to it from developer tools then remove parameter of function and put the value as "true"or "false"instead of "bx.checked"
如果您想从开发人员工具中删除它,则删除函数的参数并将值设置为“true”或“false”而不是“bx.checked”

