document.getElementById('btnid').disabled is not working in firefox and chrome

后端 未结 7 1155
醉梦人生
醉梦人生 2020-12-08 21:16

I\'m using JavaScript for disabling a button. Works fine in IE but not in FireFox and chrome, here is the script what I\'m working on:

function disbtn(e) { 
         


        
7条回答
  •  庸人自扰
    2020-12-08 21:47

    There are always weird issues with browser support of getElementById, try using the following instead:

    // document.getElementsBySelector are part of the prototype.js library available at http://api.prototypejs.org/dom/Element/prototype/getElementsBySelector/
    
    function disbtn(e) { 
        if ( someCondition == true ) {
            document.getElementsBySelector("#btn1")[0].setAttribute("disabled", "disabled");
        } else {
            document.getElementsBySelector("#btn1")[0].removeAttribute("disabled");
        }    
    }
    

    Alternatively, embrace jQuery where you could simply do this:

    function disbtn(e) { 
        if ( someCondition == true ) {
            $("#btn1").attr("disabled", "disabled");
        } else {
            $("#btn1").removeAttr("disabled");
        }    
    }
    

提交回复
热议问题