[英]How to Set checkbox properties in typescript
I am trying to fetch the checked value of group of checkboxes in a table. 我试图在表中获取复选框组的选中值。 I have a situation like 我有类似的情况
<td><input type="checkbox" value="{{my.id}}" /></td>
I have a checkbox at table header to toggle true/false for all table checkboxes 我在表头有一个复选框,可以为所有表复选框切换true / false
<th><input type="checkbox" id="checkAll" (click)="changeCheck()" [(ngModel)]="checkAllValue" /></th>
My code in changeCheck() is as below: 我在changeCheck()中的代码如下:
changeCheck(): void {
var checkedItems = jQuery("#tbPayments input[type='checkbox'][id!=checkAll]");
for (let item = 0; item < checkedItems.length; item++) {
console.log(checkedItems[item].checked = true);
}
}
But typescript throwing an error : Property 'checked ' does not exist on type 'HTMLElement' 但是打字错误的打字稿:'HTMLElement'类型中不存在属性'已检查'
How do i toggle list of checkboxes in my table. 如何在表格中切换复选框列表。 Could someone help ! 有人可以帮忙!
Cast HTMLElement
to HTMLInputElement
which has the checked
property. 将HTMLElement
转换为具有checked
属性的HTMLInputElement
。
for (let item = 0; item < checkedItems.length; item++) {
console.log((checkedItems[item] as HTMLInputElement).checked = true);
}
CheckedItems is typed to the most typed value that it can determine from the jQuery call, HTMLElement. CheckedItems被输入到可以从jQuery调用HTMLElement确定的最类型的值。 If you know it is an HTMLInputElement, you should cast it as such: 如果您知道它是HTMLInputElement,则应该将其强制转换为:
changeCheck(): void {
var checkedItems = jQuery("#tbPayments input[type='checkbox'][id!=checkAll]") as HTMLInputElement[];
for (let item = 0; item < checkedItems.length; item++) {
console.log(checkedItems[item].checked = true);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.