禁用属性的正确值是多少?

发布于 2024-11-28 07:26:52 字数 225 浏览 2 评论 0原文

文本框或文本区域的 disabled 属性的正确值是多少?

我以前见过使用以下内容:

<input type="text" disabled />
<input type="text" disabled="disabled" />
<input type="text" disabled="true" />

What is the correct value for the disabled attribute for a textbox or textarea?

I've seen the following used before:

<input type="text" disabled />
<input type="text" disabled="disabled" />
<input type="text" disabled="true" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

浅忆流年 2024-12-05 07:26:53
  • 对于 XHTML, 是有效标记。
  • 对于 HTML5, 有效并由 W3C 在其示例中使用。
  • 事实上,这两种方式都适用于所有主流浏览器。
  • For XHTML, <input type="text" disabled="disabled" /> is the valid markup.
  • For HTML5, <input type="text" disabled /> is valid and used by W3C on their samples.
  • In fact, both ways works on all major browsers.
春夜浅 2024-12-05 07:26:53

HTML5 规范

http://www.w3.org/TR/html5/forms.html#enabling-and-disabling-form-controls:-the-disabled-attribute

检查的内容属性是布尔属性

http://www.w3.org /TR/html5/infrastruct.html#boolean-attributes

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

如果该属性存在,则其值必须是空字符串或与该属性的规范名称匹配的 ASCII 不区分大小写的值,并且没有前导或尾随空格。

结论

以下是有效、等价和正确

<input type="text" disabled />
<input type="text" disabled="" />
<input type="text" disabled="disabled" />
<input type="text" disabled="DiSaBlEd" />

以下是无效

<input type="text" disabled="0" />
<input type="text" disabled="1" />
<input type="text" disabled="false" />
<input type="text" disabled="true" />

缺少该属性是唯一有效的语法>false

<input type="text" />

建议

如果您关心编写有效的 XHTML,请使用 disabled="disabled",因为 是无效的,其他替代方案的可读性较差。否则,只需使用 因为它更短。

HTML5 spec:

http://www.w3.org/TR/html5/forms.html#enabling-and-disabling-form-controls:-the-disabled-attribute :

The checked content attribute is a boolean attribute

http://www.w3.org/TR/html5/infrastructure.html#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.

If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace.

Conclusion:

The following are valid, equivalent and true:

<input type="text" disabled />
<input type="text" disabled="" />
<input type="text" disabled="disabled" />
<input type="text" disabled="DiSaBlEd" />

The following are invalid:

<input type="text" disabled="0" />
<input type="text" disabled="1" />
<input type="text" disabled="false" />
<input type="text" disabled="true" />

The absence of the attribute is the only valid syntax for false:

<input type="text" />

Recommendation

If you care about writing valid XHTML, use disabled="disabled", since <input disabled> is invalid and other alternatives are less readable. Else, just use <input disabled> as it is shorter.

初见终念 2024-12-05 07:26:53

我刚刚尝试了所有这些,对于 IE11,似乎唯一有效的是disabled =“true”。禁用值或未给出值不起作用。事实上,jsp 出现了一个错误,即所有字段都需要 equal,因此我必须指定disabled=“true”才能使其正常工作。

I just tried all of these, and for IE11, the only thing that seems to work is disabled="true". Values of disabled or no value given didnt work. As a matter of fact, the jsp got an error that equal is required for all fields, so I had to specify disabled="true" for this to work.

心凉 2024-12-05 07:26:53

MDN 上的 setAttribute():

要设置布尔属性的值,例如disabled,您可以指定任何值。建议使用空字符串或属性名称。重要的是,如果属性确实存在,无论其实际值如何,它的值都被认为是true。缺少该属性意味着其值为 false。通过将 disabled 属性的值设置为空字符串 (""),我们将 disabled 设置为 true >,这会导致按钮被禁用。

链接到 MDN

解决方案

  • 我认为的 < strong>XHTML Strict 是对的 disabled="disabled"
  • HTML5 中只是disabled,就像
  • javascript 中,我将值设置为
    true 使用 e.disabled = true;
    "" 使用 setAttribute( "disabled", "" );

在 Chrome 中测试

var f = document.querySelectorAll( "label.disabled input" );
for( var i = 0; i < f.length; i++ )
{
    // Reference
    var e = f[ i ];

    // Actions
    e.setAttribute( "disabled", false|null|undefined|""|0|"disabled" );
    /*
        <input disabled="false"|"null"|"undefined"|empty|"0"|"disabled">
        e.getAttribute( "disabled" ) === "false"|"null"|"undefined"|""|"0"|"disabled"
        e.disabled === true
    */
    
    e.removeAttribute( "disabled" );
    /*
        <input>
        e.getAttribute( "disabled" ) === null
        e.disabled === false
    */

    e.disabled = false|null|undefined|""|0;
    /*
        <input>
        e.getAttribute( "disabled" ) === null|null|null|null|null
        e.disabled === false
    */

    e.disabled = true|" "|"disabled"|1;
    /*
        <input disabled>
        e.getAttribute( "disabled" ) === ""|""|""|""
        e.disabled === true
    */
}

setAttribute() on MDN:

To set the value of a Boolean attribute, such as disabled, you can specify any value. An empty string or the name of the attribute are recommended values. All that matters is that if the attribute is present at all, regardless of its actual value, its value is considered to be true. The absence of the attribute means its value is false. By setting the value of the disabled attribute to the empty string (""), we are setting disabled to true, which results in the button being disabled.

Link to MDN

Solution

  • I think in XHTML Strict is right disabled="disabled",
  • in HTML5 is just disabled, like <input
    name="myinput" disabled>.
  • In javascript I am setting the value to
    true using e.disabled = true;
    or to "" using setAttribute( "disabled", "" );

Test in Chrome

var f = document.querySelectorAll( "label.disabled input" );
for( var i = 0; i < f.length; i++ )
{
    // Reference
    var e = f[ i ];

    // Actions
    e.setAttribute( "disabled", false|null|undefined|""|0|"disabled" );
    /*
        <input disabled="false"|"null"|"undefined"|empty|"0"|"disabled">
        e.getAttribute( "disabled" ) === "false"|"null"|"undefined"|""|"0"|"disabled"
        e.disabled === true
    */
    
    e.removeAttribute( "disabled" );
    /*
        <input>
        e.getAttribute( "disabled" ) === null
        e.disabled === false
    */

    e.disabled = false|null|undefined|""|0;
    /*
        <input>
        e.getAttribute( "disabled" ) === null|null|null|null|null
        e.disabled === false
    */

    e.disabled = true|" "|"disabled"|1;
    /*
        <input disabled>
        e.getAttribute( "disabled" ) === ""|""|""|""
        e.disabled === true
    */
}
中性美 2024-12-05 07:26:53

在 HTML5 中,没有正确的值,所有主流浏览器并不真正关心该属性是什么,它们只是检查该属性是否存在,以便禁用该元素。

In HTML5, there is no correct value, all the major browsers do not really care what the attribute is, they are just checking if the attribute exists so the element is disabled.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文