显式设置disabled =“false”在HTML中不起作用 [英] Explicitly set disabled="false" in the HTML does not work

查看:1445
本文介绍了显式设置disabled =“false”在HTML中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想明确地在html文件中将按钮设置为启用。原因是我的代码稍后将按钮切换为禁用,如果代码崩溃或者我可能会看到该按钮被禁用。



我可以通过

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

p>

 < button id =btndisabled =false>我想要启用!< / button> 

仍然显示按钮被禁用。
检查员显示:

 < button id =btndisabled =>我想成为!启用< /按钮> 

我知道我可以做

<$ p $ $(#btn)。removeAttr(disabled)

或类似的,但是对html中的许多元素做这些操作并不方便。 HTML不使用布尔值值得注意的是。



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

 < input type =复选框禁用> <! -  HTML  - > 
< input type =checkboxdisabled /> <! - 也是HTML - >
< input type =checkboxdisabled =disabled/> <! - XHTML和HTML - >

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


许多属性都是布尔属性。一个元素上布尔属性的存在表示真值,而缺少该属性则表示该值为假。



值true和false是不允许布尔属性。要表示一个错误的值,这个属性必须被完全省略。



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.

I can disable the button with

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

but then an html containing:

<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 doesn't use boolean values for boolean attributes, surprisingly.

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.

<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

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.

这篇关于显式设置disabled =“false”在HTML中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆