如何使用JavaScript禁用HTML按钮?

Jac*_*coe 173 html javascript

我已经读过你可以通过附加disable到它的标签而不是作为属性来禁用(物理上不可点击)HTML按钮,如下所示:

<input type="button" name=myButton value="disable" disabled>
Run Code Online (Sandbox Code Playgroud)

由于此设置不是属性,如何通过JavaScript动态添加此设置以禁用先前启用的按钮?

Que*_*tin 257

由于此设置不是属性

这是一个属性.

某些属性定义为boolean,这意味着您可以指定其值并将其他所有内容保留.即,仅包括粗体部分而不是禁用=" 禁用 ".在HTML 4中,您应该只包含粗体部分,因为完整版本被标记为支持有限的功能(尽管现在编写规范时不太正确).

从HTML 5开始,规则已经更改,现在只包含名称而不包含值.这没有实际区别,因为名称和值是相同的.

DOM属性也被称为disabled并且是采用布尔truefalse.

foo.disabled = true;
Run Code Online (Sandbox Code Playgroud)

从理论上讲,你还可以foo.setAttribute('disabled', 'disabled');foo.removeAttribute("disabled"),但我不会与旧版本的Internet Explorer(这是出了名的越野车,当涉及到相信这个setAttribute).

  • `foo.disabled = true;` 中的 `foo` 是什么?是那个按钮的id吗? (2认同)

小智 141

禁用

document.getElementById("btnPlaceOrder").disabled = true; 
Run Code Online (Sandbox Code Playgroud)

启用

document.getElementById("btnPlaceOrder").disabled = false; 
Run Code Online (Sandbox Code Playgroud)

  • jquery!== javascript.jquery返回一个类似数组的选择器. (6认同)
  • 我使用`$('#btnPlaceOrder')[0] .disabled = false`,因为jquery选择器似乎返回一个数组.耸肩. (4认同)
  • 出于某种原因,这对我有用,即使`$('#btnPlaceOrder').disabled = false;`却没有. (3认同)

And*_*y E 22

它是一个属性,但是一个布尔值(因此它不需要名称,只需要一个值 - 我知道,这很奇怪).您可以在Javascript中设置等效的属性:

document.getElementsByName("myButton")[0].disabled = true;
Run Code Online (Sandbox Code Playgroud)

  • 它确实需要一个值,它是不需要的名称.(奇怪,但确实如此). (2认同)

Mak*_*yuk 10

请尝试以下方法:

document.getElementById("id").setAttribute("disabled", "disabled");
Run Code Online (Sandbox Code Playgroud)

  • 正如[David Dorward提到的](http://stackoverflow.com/questions/3014649/how-to-disable-html-button-using-javascript/3014678#3014678),这不能依赖于正确的跨浏览器和属性应该使用等效的. (4认同)

Pat*_*rts 7

disabled在an 上设置属性的官方方法HTMLInputElement是:

var input = document.querySelector('[name="myButton"]');
// Without querySelector API
// var input = document.getElementsByName('myButton').item(0);

// disable
input.setAttribute('disabled', true);
// enable
input.removeAttribute('disabled');
Run Code Online (Sandbox Code Playgroud)

虽然@ kaushar的答案足以启用和禁用HTMLInputElement,并且由于IE历史上的错误而可能更适合跨浏览器兼容性setAttribute,但它只能用于Element属性阴影Element属性.如果设置了属性,那么DOM默认使用属性的值而不是等效属性的值.

属性和属性之间存在非常重要的差异.真实HTMLInputElement 属性的一个示例是input.value,下面演示了阴影的工作原理:

var input = document.querySelector('#test');

// the attribute works as expected
console.log('old attribute:', input.getAttribute('value'));
// the property is equal to the attribute when the property is not explicitly set
console.log('old property:', input.value);

// change the input's value property
input.value = "My New Value";

// the attribute remains there because it still exists in the DOM markup
console.log('new attribute:', input.getAttribute('value'));
// but the property is equal to the set value due to the shadowing effect
console.log('new property:', input.value);
Run Code Online (Sandbox Code Playgroud)
<input id="test" type="text" value="Hello World" />
Run Code Online (Sandbox Code Playgroud)

这就是说属性影子属性的含义.此概念也适用于prototype链上的继承属性:

function Parent() {
  this.property = 'ParentInstance';
}

Parent.prototype.property = 'ParentPrototype';

// ES5 inheritance
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;

function Child() {
  // ES5 super()
  Parent.call(this);

  this.property = 'ChildInstance';
}

Child.prototype.property = 'ChildPrototype';

logChain('new Parent()');

log('-------------------------------');
logChain('Object.create(Parent.prototype)');

log('-----------');
logChain('new Child()');

log('------------------------------');
logChain('Object.create(Child.prototype)');

// below is for demonstration purposes
// don't ever actually use document.write(), eval(), or access __proto__
function log(value) {
  document.write(`<pre>${value}</pre>`);
}

function logChain(code) {
  log(code);

  var object = eval(code);

  do {
    log(`${object.constructor.name} ${object instanceof object.constructor ? 'instance' : 'prototype'} property: ${JSON.stringify(object.property)}`);
    
    object = object.__proto__;
  } while (object !== null);
}
Run Code Online (Sandbox Code Playgroud)

我希望这可以澄清关于属性和属性之间差异的任何混淆.


Oli*_*Oli 5

它仍然是一个属性。将其设置为:

<input type="button" name=myButton value="disable" disabled="disabled">
Run Code Online (Sandbox Code Playgroud)

... 已验证。

  • 所有浏览器都在寻找 disabled="disabled",我知道规范说要避免它,但我从来没有遇到过设置 disabled="disabled" 或 checked="checked" 或 selected="selected" 的问题......只是不要t do disabled="true"...只有一些浏览器会识别 (2认同)