Button disabled attribute

Definition and usage

disabled Attribute sets or returns whether the button is disabled.

Disabled elements are not available and cannot be clicked. By default, disabled elements are usually displayed in gray in the browser.

This attribute reflects HTML disabled attribute.

Example

Example 1

Disable button:

document.getElementById("myBtn").disabled = true;

Try it yourself

Example 2

Check if the button is disabled:

var x = document.getElementById("myBtn").disabled;

Try it yourself

Example 3

Disable and enable button:

function disableBtn() {
    document.getElementById("myBtn").disabled = true;
}
function enableBtn() {
    document.getElementById("myBtn").disabled = false;
}

Try it yourself

Syntax

Return disabled attribute:

buttonObject.disabled

set disabled attribute:

buttonObject.disabled = true|false

属性值

描述
true|false

规定是否应禁用按钮。

  • true - 按钮被禁用
  • false - 默认。该按钮未禁用

技术细节

返回值: 布尔值,如果按钮被禁用则返回 true,否则返回 false。

浏览器支持

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
支持 支持 支持 支持 支持

相关页面

HTML 参考手册:HTML <button> disabled অপারেশন