打字稿选择/取消选择所有复选框

时间:2019-01-15 15:27:07

标签: typescript

我需要在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();
    }
  }

}

1 个答案:

答案 0 :(得分:0)

您无需单击复选框即可取消选中它。只需将checked属性设置为false即可:

(< HTMLInputElement > locationElements[i]).checked = false;