简体   繁体   English

在纯 javascript 中启用/禁用按钮

[英]Enable/disable a button in pure javascript

I want to enable/disable a button without jquery.我想在没有 jquery 的情况下启用/禁用按钮。 Here's my code:这是我的代码:

btn.setAttribute("disabled", true);

Works.作品。 But this doesn't -- a button remains disabled:但这不是 - 一个按钮保持禁用状态:

btn.setAttribute("disabled", false);

disabled is a Boolean attribute. disabled是一个布尔属性。 The mere presence of it causes the element to be disabled regardless of what the value of that attribute actually is.无论该属性的实际值是什么,它的存在都会导致元素被禁用。 This is why you are able to disable the element in JavaScript by setting the attribute to true , you could have set it to anything (and that is the reason why when you set it to false it remains disabled).这就是为什么您可以通过将属性设置为true来禁用 JavaScript 中的元素,您可以将其设置为任何内容(这就是为什么将其设置为false它仍然处于禁用状态的原因)。

 <input type="button" value="I'm disabled" disabled="true"> <input type="button" value="I'm disabled" disabled="false"> <input type="button" value="I'm disabled" disabled="doesn't matter"> <input type="button" value="I'm disabled" disabled="">

With Boolean attributes, you don't even need to set a value at a for the attribute at all:使用 Boolean 属性,您甚至根本不需要为属性设置 a 值:

 <input type="button" value="I'm disabled" disabled>

However the recommended convention with Boolean attributes (if you do want to provide a value for the attribute), so that we can have some consistency among developers, is to set their value equal to the attribute name itself.然而,推荐的布尔属性约定(如果你确实想为属性提供一个值),以便我们在开发人员之间有一些一致性,是将它们的值设置为等于属性名称本身。 So, to disable an element, by working with its attribute, in JavaScript, following recommended conventions:因此,要在 JavaScript 中通过使用其属性来禁用元素,请遵循推荐的约定:

element.setAttribute("disabled", "disabled");

Therefore, to enable an element, you don't set the disabled attribute to any value, because as we've seen, that will just disabled it, you need to remove the disabled attribute completely:因此,要启用一个元素,您不要将disabled属性设置为任何值,因为正如我们所见,这只会禁用它,您需要完全删除disabled属性:

element.removeAttribute("disabled");

 document.querySelector("input[type='button']").removeAttribute("disabled");
 <input type="button" value="I'm NOT disabled" disabled="disabled">


Now, when working with DOM objects in JavaScript, there are two ways to affect the current state of an element and it's important to understand the effects of working with these two techniques:现在,在 JavaScript 中使用 DOM 对象时,有两种方法可以影响元素的当前状态,了解使用这两种技术的效果很重要:

  1. Work with the element's current HTML state (which is done with setAttribute() , removeAttribute() , and getAttribute() ).使用元素的当前 HTML 状态(通过setAttribute()removeAttribute()getAttribute() )。
  2. Work with element's current DOM Object state (which is done with the JavaScript property that represents the current state) of the element in memory.使用元素在内存中的当前 DOM 对象状态(通过代表当前状态的 JavaScript 属性完成)。

Most importantly, the JavaScript object property value can be different than the HTML element attribute value.最重要的是, JavaScript 对象属性值可以与HTML 元素属性值不同。 This can be confusing but the HTML state is what the element looks like from the outside and the property state is what is really happening on the inside, like you can put a happy face on so that people who look at you think your happy (the HTML state), but you might actually be sad for real (the property state).这可能会令人困惑,但 HTML 状态是元素从外部看起来的样子,属性状态是内部真正发生的事情,就像你可以装出一张快乐的脸,这样看着你的人就会认为你很快乐( HTML 状态),但您实际上可能会为真实(属性状态)感到难过。

When the property state hasn't been set, the attribute state is all that matters and will have total control over the state of the element.当尚未设置属性状态时,属性状态才是最重要的,并且将完全控制元素的状态。 When the property state is set, it overrides whatever the attribute state may be and controls the actual state of the element.设置属性状态后,它会覆盖可能是的属性状态并控制元素的实际状态。

 // Get a reference to the button var btn = document.querySelector("[type=button]"); // Test what the current HTML state is: console.log(btn.getAttribute("disabled")); // Test what the current mapped property state is: console.log(btn.disabled); // Change the property state, which will override the HTML state and // and cause it to become enabled. btn.disabled = false; // Test what the current HTML state is: console.log(btn.getAttribute("disabled")); // null because property overrode HTML // Test what the current mapped property value is: console.log(btn.disabled);
 <input type="button" value="I'm disabled" disabled="disabled">

From MDN :来自MDN

To enable the element, leave this attribute out entirely as opposed to setting the value to false .要启用该元素,请完全保留此属性,而不是将值设置为false

 function getElement(elm){ return document.getElementById(elm); } /*-------------FUNCTION TO DISABLE AN TEXT BOX------------------*/ function disable(elm){ return getElement(elm).setAttribute("disabled", true); } //==============================================================// /*--------------FUNCTION TO ENABLE AN TEXT BOX------------------*/ function enable(elm){ return getElement(elm).removeAttribute("disabled"); } //==============================================================// function disableEnable(){ if(getElement("button").disabled){ enable("button"); enable("input-button"); } else{ disable("button"); disable("input-button"); } }
 <button id="button">Button</button> <input id="input-button" type="button" value="Input Button"/> <br/><br/><br/> <button onClick="disableEnable();"> Disable/Enable Buttons Above</button>

btn.removeAttribute("disabled");
element.disabled = true
element.disabled = false

This is perfectly valid and works as you'd expect - ie does not disable the element when set to true as the accepted answer suggests.这是完全有效的并且可以按您的预期工作 - 即在设置为 true 时不会禁用元素,如已接受的答案所建议的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM