1 頁 (共 1 頁)

[javascript][console]如何將頁面上的checkbox都勾選

發表於 : 2014年 6月 16日, 10:12
tim
參考這篇在 stackoverflow 的資料http://stackoverflow.com/questions/1090 ... or-console, 以及這篇資料 http://nathangiesbrecht.com/check-all-c ... javascript

使用 javascript 的方式

代碼: 選擇全部

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 的方式

代碼: 選擇全部

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