How to enable a disabled checkbox dynamically?

前端 未结 3 1199
遇见更好的自我
遇见更好的自我 2021-02-04 05:03

Please see here: http://jsfiddle.net/nShQs/

Press the disable button and then the enable button. The checkbox doesn\'t get enabled.

HTML:



        
相关标签:
3条回答
  • 2021-02-04 05:17

    Just do

    function enable() {
        document.getElementById("check").disabled= false;
    
    }
    
    function disable() {
         document.getElementById("check").disabled= true;
    }
    

    With this you are setting the property of the DOM element, while setting attribute presence of attribute disabled will disable the check box, so even if you do x.setAttribute("disabled", "false"); it will still be there on the element as attribute.

    Demo

    or you would just do:

    function disable() {
        document.getElementById("check").setAttribute('disabled', 'disabled');
    }
    
    function enable() {
       document.getElementById("check").removeAttribute('disabled');
    }
    

    disabled as attribute and disabled as property are different.

    0 讨论(0)
  • Set the disabled property rather than the attribute (fiddle).

    function enable() {
        document.getElementById("check").disabled = false;    
    }
    
    function disable() {
        document.getElementById("check").disabled = true;
    }
    

    A control will remain disabled if the disabled attribute is present at all - regardless of its value (fiddle). Setting the disabled property to false will remove the disabled attribute.

    0 讨论(0)
  • 2021-02-04 05:27

    It works,

     x.removeAttribute("disabled");
    

    http://jsfiddle.net/maximos/89wxX/1/

    0 讨论(0)
提交回复
热议问题