在 HTML 中显式设置 disabled="false" 不起作用

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

Explicitly set disabled="false" in the HTML does not work

htmldisabled-control

提问by aless80

I would like to explicitly set a button as enabled in the html file. The reason is that my code later on toggles the button to disabled and if the code crashes or so I might see the button disabled.

我想在 html 文件中显式设置一个按钮为启用状态。原因是我的代码稍后将按钮切换为禁用状态,如果代码崩溃,我可能会看到按钮被禁用。

I can disable the button with

我可以禁用按钮

$("#btn").attr("disabled","true")

but then an html containing:

但随后的 html 包含:

<button id="btn" disabled="false">I want to be enabled!</button>

still shows the button as disabled. The inspector shows:

仍将按钮显示为禁用。检查员显示:

<button id="btn" disabled="">I want to be enabled!</button>

I know I can do

我知道我能做到

$("#btn").removeAttr("disabled") 

or similar, but it is not convenient to do that for many elements in the html.

或类似的,但对 html 中的许多元素这样做并不方便。

回答by Dai

HTML doesn't use boolean values for boolean attributes, surprisingly.

令人惊讶的是,HTML 不使用布尔值作为布尔属性。

In HTML, boolean attributes are specified by either merely adding the attribute name, or (especially in XHTML) by using the attribute name as its value.

在 HTML 中,布尔属性是通过仅添加属性名称或(尤其是在 XHTML 中)使用属性名称作为其值来指定的。

<input type="checkbox" disabled>                 <!-- HTML -->
<input type="checkbox" disabled />               <!-- Also HTML -->
<input type="checkbox" disabled="disabled" />    <!-- XHTML and HTML -->

This is documented in the HTML specification: http://www.w3.org/TR/html5/infrastructure.html#boolean-attribute

这记录在 HTML 规范中:http: //www.w3.org/TR/html5/infrastructure.html#boolean-attribute

A number of attributes are boolean attributes. The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value.

The values "true" and "false" are not allowed on boolean attributes. To represent a false value, the attribute has to be omitted altogether.

许多属性是布尔属性。元素上布尔属性的存在表示真值,不存在该属性表示假值。

布尔属性不允许使用值“true”和“false”。要表示假值,必须完全省略该属性。

回答by Alex

In future, can help someone.

将来,可以帮助某人。

selectLanguage.onchange = function() {
    var value = selectLanguage.value;
    if (value != '') {
        submitLanguage.removeAttr('disabled');
    } else {
        submitLanguage.attr('disabled', 'disabled');
    }
    // submitLanguage.attr('enabled', 'enabled');
}

回答by Adsy2010

I know this is an old topic but as there is no marked answer, does this help? This does answer the explicitly marked as enabled question.

我知道这是一个古老的话题,但由于没有明确的答案,这有帮助吗?这确实回答了明确标记为启用的问题。

<button enabled>My Text</button>
<!-- Which also works as: -->
<button enabled="enabled">My Text</button>

I too am investigating this for use to enabled a button when validation occurs. I hope this helps someone.

我也在调查这个用于在验证发生时启用按钮。我希望这可以帮助别人。

回答by Daniel Perník

If you are using AngularJS, try ng-disabledinstead. In this case you can use stuff like:

如果您正在使用AngularJS,请尝试ng-disabled。在这种情况下,您可以使用以下内容:

ng-disabled="true"
ng-disabled="false"
ng-disabled={{expression}}

and it works as expected....

它按预期工作......

回答by Alexander Ramos

Si esta usando Vuejs

Si esta usando Vuejs

:disabled="true" //desactiva el input
:disabled="false" //activa el input