简体   繁体   English

如何在typescript中设置复选框属性

[英]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 我有类似的情况

  • List all the records in a table with each row having a checkbox 列出表中的所有记录,每行都有一个复选框

 <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.

相关问题 如何在js中的复选框数组上将选中的属性设置为false - How to set checked properties false on checkbox array in js TypeScript:如何在构造函数中设置对象属性(取决于对象属性) - TypeScript: How to set object property in constructor (depending on object properties) 如何使用setter和getter在TypeScript中正确设置对象属性? - How to properly set object properties in TypeScript with setters and getters? Typescript是否在类构造函数中设置接口属性? - Does Typescript set interface properties in the Class Constructor? TypeScript:动态设置类型化 object 属性 - TypeScript: Dynamically set typed object properties 如何在TypeScript中创建值属性 - How to create value properties in TypeScript 如何总结 typescript 中 object 的属性? - How to sum the properties of an object in typescript? 如何使用TypeScript根据另一个属性将React组件的两个属性设置为可选属性? - How to set two properties for a React component as optional depending on another property using TypeScript? 如何键入接受具有超集属性的上下文的 Typescript React 组件 - How to type a Typescript React Component that accepts a Context with a super-set of properties 如何制作 React 和 Typescript 以允许基本属性集或扩展属性集 - How to make React and Typescript to allow either base set of properties or an extended one
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM