【问题标题】:How to disable HTML button using JavaScript?如何使用 JavaScript 禁用 HTML 按钮?
【发布时间】:2011-03-02 04:11:58
【问题描述】:

我了解到您可以通过将disable 附加到其标签而不是作为属性来禁用(使物理上无法点击)HTML 按钮,如下所示:

<input type="button" name=myButton value="disable" disabled>

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

【问题讨论】:

    标签: javascript html


    【解决方案1】:

    由于此设置不是属性

    这是一个属性。

    某些属性被定义为布尔值,这意味着您可以指定它们的值而忽略其他所有内容。即,您只包括粗体部分,而不是 disabled="disabled"。在 HTML 4 中,您应该只包含粗体部分,因为完整版本被标记为 a feature with limited support(尽管现在这在编写规范时不太正确)。

    从 HTML 5 开始,规则发生了变化,现在您只包含名称而不包含值。这没有实际区别,因为名称和值是相同的。

    DOM property 也称为 disabled,是一个布尔值,采用 truefalse

    foo.disabled = true;
    

    理论上你也可以foo.setAttribute('disabled', 'disabled');foo.removeAttribute("disabled"),但我不相信旧版本的Internet Explorer 会这样(众所周知,setAttribute 存在漏洞)。

    【讨论】:

    • 两者兼而有之,更改属性并设置属性是否有意义,还是只是矫枉过正?
    • foo 中的 foo.disabled = true; 是什么?是那个按钮的id吗?
    • @stack — 一个变量,包含对通过您喜欢的任何方式(例如 querySelector)收集的元素的引用
    【解决方案2】:

    禁用

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

    启用

    document.getElementById("btnPlaceOrder").disabled = false; 
    

    【讨论】:

    • 由于某种原因这对我有用,即使$('#btnPlaceOrder').disabled = false; 没有。
    • 我使用 $('#btnPlaceOrder')[0].disabled = false 作为 jquery 选择器似乎返回一个数组。耸耸肩。
    • jquery !== javascript。 jquery 返回一个类似数组的选择器。
    • @levininja, Chilly — $('#btnPlaceOrder').prop('disabled',false);
    【解决方案3】:

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

    document.getElementsByName("myButton")[0].disabled = true;
    

    【讨论】:

    • 它确实需要一个值,它是它不需要的名称。 (奇怪,但真实)。
    • @David:我记得以前读过,已修复。这确实很奇怪,如果语法突出显示正确地兑现它可能会更有意义:-)
    【解决方案4】:

    尝试以下方法:

    document.getElementById("id").setAttribute("disabled", "disabled");
    

    【讨论】:

    【解决方案5】:

    HTMLInputElement 上设置disabled 属性的官方方法是这样的:

    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');
    

    虽然@kaushar's answer 足以启用和禁用HTMLInputElement,并且由于IE 历史上存在错误setAttribute,它可能更适合跨浏览器兼容性,但它仅适用于Element 属性遮蔽Element 属性。如果设置了属性,则 DOM 默认使用该属性的值,而不是等效属性的值。

    属性和属性之间有一个非常重要的区别。 input.value 是真正的HTMLInputElement 属性 示例,下面演示了阴影的工作原理:

    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);
    &lt;input id="test" type="text" value="Hello World" /&gt;

    这就是说属性影子属性的意思。这个概念也适用于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);
    }

    我希望这可以澄清关于属性和属性之间区别的任何混淆。

    【讨论】:

      【解决方案6】:

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

      <input type="button" name=myButton value="disable" disabled="disabled">
      

      ...有效。

      【讨论】:

      • 有效,但规范说应该避免使用语法:w3.org/TR/html4/appendix/notes.html#h-B.3.3
      • 所有浏览器都在寻找 disabled="disabled",我知道规范说要避免它,但我在设置 disabled="disabled" 或 check="checked" 或 selected=" 时从未遇到任何问题selected"...只是不要这样做 disabled="true"...只有某些浏览器会识别出来
      • 显然并非所有浏览器都支持它——如果不支持,规范就不会那么明确。它们只是不再常用。
      • 顺便说一句,问题是“如何通过 JavaScript 动态添加它”
      • 当然,但问题继续讨论禁用不是属性。我的回答是消除它并说您可以将其用作属性。您可以也使用 DOM 属性。
      【解决方案7】:

      如果你有按钮对象,叫做 b:b.disabled=false;

      【讨论】:

        【解决方案8】:

        我认为最好的方法可能是:

        $("#ctl00_ContentPlaceHolder1_btnPlaceOrder").attr('disabled', true);
        

        跨浏览器运行良好。

        【讨论】:

        • 应该是prop,而不是attr
        • 问题是寻找原生js解决方案
        【解决方案9】:
        <button disabled=true>text here</button>
        

        您仍然可以使用属性。只需使用“禁用”属性而不是“值”。

        【讨论】:

        • 它是 disabled="disabled" 或只是 disabled。任何字符串值都等价于"disabled",包括disabled="true"disabled="false"
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-07-22
        • 1970-01-01
        • 1970-01-01
        • 2013-02-09
        • 2021-08-06
        • 2022-11-23
        • 1970-01-01
        相关资源
        最近更新 更多