检查复选框元素是否在 TypeScript 中被选中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20043265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:09:24  来源:igfitidea点击:

Check if checkbox element is checked in TypeScript

typescript

提问by Yaniv

I have a checkbox on my html file.

我的 html 文件上有一个复选框。

<input id="is3dCheckBox" type="checkbox" checked="checked" />

and I want to know in the .ts file if this checkbox is checked or not.
What cast should be done after I get this element in order to check it?

我想在 .ts 文件中知道这个复选框是否被选中。
在我得到这个元素之后应该做什么样的演员来检查它?

回答by Fenton

You just need to use a type assertionto tell TypeScript it is an HTMLInputElement:

你只需要使用类型断言来告诉 TypeScript 它是一个 HTMLInputElement:

var element = <HTMLInputElement> document.getElementById("is3dCheckBox");
var isChecked = element.checked;

Update:

更新:

Any ideas why var element = document... works but var element: HTMLInputElement = document... does not?

任何想法为什么 var element = document... 有效但 var element: HTMLInputElement = document... 没有?

When you use getElementByIdthe element returned could be any kind of HTML element. For example, it could be a paragraph tag. For this reason, the type definition for this DOM method returns a very general HTMLElementtype.

使用时getElementById返回的元素可以是任何类型的 HTML 元素。例如,它可能是一个段落标签。因此,此 DOM 方法的类型定义返回一个非常通用的HTMLElement类型。

When you try the following:

当您尝试以下操作时:

var element: HTMLInputElement = document.getElementById('is3dCheckBox');

The compiler warns you that the result of getElementByIdis not an HTMLInputElement, or a sub-type of HTMLInputElement(because it is actually a super-type of the one you want).

编译器警告您,结果getElementById不是HTMLInputElement,也不是 的子类型HTMLInputElement(因为它实际上是您想要的类型的超类型)。

When you know the element type, it is perfectly normal to use a type assertion to tell the compiler the type. When you use the Type Assertion, you tell the compiler you know better.

当您知道元素类型时,使用类型断言告诉编译器类型是完全正常的。当您使用类型断言时,您告诉编译器您更了解。

In some cases, the compiler will stillwarn you when using a type assertion, because it can tell that the assertion is likely to be a mistake - but you can still force it, but widening the type first:

在某些情况下,编译器在使用类型断言时仍会警告您,因为它可以告诉您该断言很可能是错误的——但您仍然可以强制它,但首先扩大类型:

var isChecked = (<HTMLInputElement><any>myString).checked;

The string variable is widened to the anytype, before asserting the HTMLInputElement. Hopefully you won't need to use this kind of type assertion too often.

字符串变量被加宽到any类型,断言之前HTMLInputElement。希望您不需要经常使用这种类型的断言。

回答by Elydasian

For documet.getElementById you can use:

对于 documet.getElementById,您可以使用:

let element = <HTMLInputElement> document.getElementById("is3dCheckBox");  
if (element.checked) { you code }

For documet.getElementsByName you can use:

对于 documet.getElementsByName,您可以使用:

let element = <any> document.getElementsByName("is3dCheckBox");  
if (element.checked) { you code }