如何启用/禁用 TypeScript 1.5 中的按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31635975/
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-09 06:44:04  来源:igfitidea点击:

How to enble/disable a button in TypeScript 1.5?

typescripttypescript1.5

提问by lucobada

Using VS 2013. After having installed TypeScript 1.5 and following the question/suggestion to upgrade:

使用 VS 2013。安装 TypeScript 1.5 并按照问题/建议升级后:

"Your project uses a version of TypeScript older than the version currently installed with Visual Studio. You may get errors if you try to build your project. Would you like us to upgrade the TypeScriptToolsVersion in your project file so you don't see this warning again?"

“您的项目使用的 TypeScript 版本比当前随 Visual Studio 安装的版本旧。如果您尝试构建项目,可能会出错。您是否希望我们升级项目文件中的 TypeScriptToolsVersion 以便您看不到此警告再次?”

I got a bunch of errors like:

我收到了一堆错误,例如:

Error 39 Build: Property 'disabled' does not exist on type 'HTMLElement'.

错误 39 构建:“HTMLElement”类型上不存在“禁用”属性。

on statements like:

关于以下语句:

document.getElementById("btnExcel").disabled = false;

with 'disabled' curly underlined.

带有“禁用”卷曲下划线。

On https://github.com/Microsoft/TypeScript/wiki/Breaking-Changesreferring to version 1.5 it says: "Properties on resize, disabled, uniqueID, removeNode, fireEvent, currentStyle, runtimeStyle are removed from type HTMLElement"

https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes 上提到版本 1.5,它说:“调整大小、禁用、uniqueID、removeNode、fireEvent、currentStyle、runtimeStyle 的属性从 HTMLElement 类型中删除”

Now I rephrased those "erroneous" statements like this:

现在我将那些“错误”的陈述改写如下:

document.getElementById("btnExcel").setAttribute('disabled', 'disabled');

which to me looks weird.

这对我来说看起来很奇怪。

Can this be expressed more elegantly in a typesafe way in TypeScript 1.5? Can you give examples for both: enabling and disabling?

这可以在 TypeScript 1.5 中以类型安全的方式更优雅地表达吗?你能举出两个例子:启用和禁用吗?

Thanks for any help!

谢谢你的帮助!

回答by Martin

There should be a cleaner way to do this:

应该有一种更干净的方法来做到这一点:

var element = <HTMLInputElement> document.getElementById("btnExcel");
element.disabled = true;

Or if you prefer a one liner:

或者,如果您更喜欢单衬:

(<HTMLInputElement> document.getElementById("btnExcel")).disabled = true;

It seems getElementByIdand the like should be a generic method and accept a type parameter. That said, a generic method wouldn't give you anything that typecasting doesn't.

看来getElementById之类的应该是一个泛型方法并接受一个类型参数。也就是说,泛型方法不会给你任何类型转换没有的东西。

回答by Rochana Pathiraja

I'm using TypeScript v 3.7.something. In here

我正在使用 TypeScript v 3.7.something。在这里

(document.getElementById('button') as HTMLInputElement).disabled = false;

is prefered. (guided by @Martin's answer)

首选。(以@Martin 的回答为指导)