通过javascript或控制台选择全部复选框(Select All checkbox by javascript or console)

2135 IT屋

I am having 100 Checkboxes on my webpage. 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?

Perhaps a JavaScript or Chrome Console window, anything?

解决方案

The most direct way would be to grab all your inputs, filter just the checkboxes out, and set the checked property.

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

$("input[type='checkbox']").prop("checked", true);

or as Fabricio points out:

$(":checkbox").prop("checked", true);

我的网页上有100个复选框。为了测试目的,我想勾选所有这些框,但手动点击是耗时的。



可能是JavaScript或Chrome控制台窗口,什么?


解决方案

最直接的方法是抓取所有的输入,只过滤复选框,并设置checked属性。



  var allInputs = document.getElementsByTagName(input); 
for(var i = 0,max = allInputs.length; i if(allInputs [i] .type ==='checkbox')
allInputs [ i] .checked = true;
}





使用jQuery - 我不是说你应该开始只是勾选所有的复选框进行测试 - 你可以简单地做



  $ (input [type ='checkbox'])。prop(checked,true); 


或作为Fabricio指出:



  $(:checkbox)。prop(checked,true); 

本文地址:IT屋 » 通过javascript或控制台选择全部复选框