通过 Javascript 或控制台全选复选框 [英] Select All checkbox by Javascript or console

查看:20
本文介绍了通过 Javascript 或控制台全选复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网页上有 100 个复选框.出于测试目的,我想勾选所有这些框,但手动单击非常耗时.有没有办法让他们打勾?

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?

也许是 JavaScript 或 Chrome 控制台窗口,什么的?

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;
}

<小时>

如果你碰巧在使用 jQuery——我并不是说你应该开始只是勾选所有的复选框进行测试——你可以简单地做


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);

这篇关于通过 Javascript 或控制台全选复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆