我需要在Typescript中选中/取消选中所有复选框。下面的代码在选中时将全选,而在未选中时不会取消全选。
selectAllLocations() {
var selectAll = < HTMLInputElement > document.getElementById("allLocations");
let locationElements: NodeListOf < Element > = document.getElementsByClassName("route-location-list");
for (var i = 0; i < locationElements.length; i++) {
if (selectAll.checked && (( < HTMLInputElement > locationElements[i]).checked == false)) {
( < HTMLInputElement > locationElements[i]).click();
} else if ((!selectAll.checked) && ( < HTMLInputElement > locationElements[i]).checked) {
( < HTMLInputElement > locationElements[i]).click();
}
}
}
答案 0 :(得分:0)
您无需单击复选框即可取消选中它。只需将checked
属性设置为false
即可:
(< HTMLInputElement > locationElements[i]).checked = false;