通过 Javascript 或控制台全选复选框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10908212/
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
Select All checkbox by Javascript or console
提问by Yahoo
I am having 100 Checkboxes on my web page. For testing purposes I want to tick all those boxes, but manually clicking is time consuming. Is there a possible way to get them ticked?
我的网页上有 100 个复选框。出于测试目的,我想勾选所有这些框,但手动单击非常耗时。有没有办法让他们打勾?
Perhaps a JavaScript or Chrome Console window, anything?
也许是 JavaScript 或 Chrome 控制台窗口,什么的?
回答by Adam Rackis
The most direct way would be to grab all your inputs, filter just the checkboxes out, and set the checked property.
最直接的方法是获取所有输入,仅过滤复选框,然后设置 checked 属性。
var allInputs = document.getElementsByTagName("input");
for (var i = 0, max = allInputs.length; i < max; i++){
if (allInputs[i].type === 'checkbox')
allInputs[i].checked = true;
}
If you happen to be using jQuery—and I'm not saying you should start just to tick all your checkboxes for testing—you could simply do
如果你碰巧使用 jQuery——我并不是说你应该开始只是勾选所有的复选框进行测试——你可以简单地做
$("input[type='checkbox']").prop("checked", true);
or as Fabricio points out:
或者正如法布里西奥指出的那样:
$(":checkbox").prop("checked", true);
回答by Loktar
Pure JS method, don't use jQuery.. its just silly for something so trivial.
纯 JS 方法,不要使用 jQuery .. 对于如此微不足道的事情来说这只是愚蠢的。
[].forEach.call( document.querySelectorAll('input[type="checkbox"]'),function(el){
el.checked=true;
}
);?
To use it on any webpage you can paste this into the address bar
要在任何网页上使用它,您可以将其粘贴到地址栏中
javascript:[].forEach.call(document.querySelectorAll('input[type="checkbox"]'),function(el){el.checked=true});
then drag that to your bookmarks, and you have a bookmarklet. Just click it whenever you need to use it on a page.
然后把它拖到你的书签上,你就有了一个书签。只要您需要在页面上使用它,只需单击它。
回答by Talha
回答by u283863
querySelectorAll
is your best choice here if you don't want jQuery!
querySelectorAll
如果您不想要 jQuery,那么这里是您的最佳选择!
var ele = document.querySelectorAll("input[type=checkbox]");
for(var i=0;i<ele.length;i++){
ele[i].checked = true;
}
//Done.
回答by Michael
Multiple Check All & Uncheck All Boxes
多个选中所有和取消选中所有框
All You Need to change is the tag 'name' to change the what its turing ON/OFF
所有你需要改变的是标签“名称”来改变它的图灵开/关
<FORM>
<input type="checkbox" name="item0[]" onclick="CheckAll(this)" />
<input type="checkbox" name="item0[]" value="1" />
<input type="checkbox" name="item0[]" value="2" />
<input type="checkbox" name="item0[]" value="3" />
<br>
<input type="checkbox" name="item1[]" onclick="CheckAll(this)" />
<input type="checkbox" name="item1[]" value="a" />
<input type="checkbox" name="item1[]" value="b" />
<input type="checkbox" name="item1[]" value="c" />
<br>
<input type="checkbox" name="item2" onclick="CheckAll(this)" />
<input type="checkbox" name="item2" value="a1" />
<input type="checkbox" name="item2" value="b2" />
<input type="checkbox" name="item2" value="bc" />
</FORM>
<script>
function CheckAll(x)
{
var allInputs = document.getElementsByName(x.name);
for (var i = 0, max = allInputs.length; i < max; i++)
{
if (allInputs[i].type == 'checkbox')
if (x.checked == true)
allInputs[i].checked = true;
else
allInputs[i].checked = false;
}
}
</script>
回答by Fabrício Matté
This JS code will check all checkboxed in your page:
此 JS 代码将检查您页面中的所有复选框:
var a = document.querySelectorAll('input[type="checkbox"]');
for (var i=0; i<a.length; i++)
a[i].checked = true;?
All you have to do then is create a bookmarklet with it, say, with this bookmarklet maker, which generates this bookmarklet code:
然后你所要做的就是用它创建一个书签,比如说,使用这个 bookmarklet maker,它会生成这个书签代码:
javascript:var a=document.querySelectorAll('input[type="checkbox"]');for(var i=0;i<a.length;i++)a[i].checked=true;%E2%80%8B
Just add this URI to a bookmark in your bookmark toolbar, then all you have to do is click it whenever you need all the checkboxes in your page to be checked. =]
只需将此 URI 添加到书签工具栏中的书签,然后您只需在需要检查页面中的所有复选框时单击它即可。=]
回答by Vlad Bezden
I provided answer to this question at Check all Checkboxes in Page via Developer Tools
我在 Check all Checkboxes in Page via Developer Tools提供了这个问题的答案
In short you can do it from dev tools console (F12) by:
简而言之,您可以通过以下方式从开发工具控制台 (F12) 执行此操作:
$$('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 Amritpal Singh
function selectAll(elem)
{
for (i = 0; i < elem.length; i++)
elem[i].checked = true ;
}
On Click of a button call this method and pass the name of the element(checkboxes-they all should be same named).
单击按钮时调用此方法并传递元素的名称(复选框 - 它们都应具有相同的名称)。