How do I disable all input buttons without using jQuery?

后端 未结 4 1429
野的像风
野的像风 2020-12-25 15:26

How would I do this without jQuery?

$(\'input[type=submit]\').attr(\'disabled\',true);

It doesn\'t have to be cross-browser compatible; a s

相关标签:
4条回答
  • 2020-12-25 15:40

    Have you tried

    document.getElementsByTagName("input");
    

    then you could interrogate the DOM to find your submit button. getElementsByTagName reference

    A full sample

    window.onload = function(e) {
        var forms = document.getElementsByTagName('form');
        for (var i = 0; i < forms.length; i++) {
            var input = forms[i].getElementsByTagName('input');
            for (var y = 0; y < input.length; y++) {
                if (input[y].type == 'submit') {
                    input[y].disabled = 'disabled';
                }
            }
    
        }
    }
    
    0 讨论(0)
  • 2020-12-25 15:42

    This is untested, but it or something very similar should work. It could be made better with error and feature checking.

    var inputs = document.getElementsByTagName('input');
    
    for(var i = 0; i < inputs.length; i++){
      if(inputs[i].type == 'submit'){
        inputs[i].disabled = 'disabled';
      }
    }
    
    0 讨论(0)
  • 2020-12-25 16:00
    var els = document.getElementsByTagName ( 'input' );
    for ( var i = 0; i < els.length ; i ++ ) {
     if ( els[i].type == 'submit' ) els[i].setAttribute('disabled', 'true'); 
    }
    
    0 讨论(0)
  • 2020-12-25 16:02
    var inputs = document.getElementsByTagName("INPUT");
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].type === 'submit') {
            inputs[i].disabled = true;
        }
    }
    
    0 讨论(0)
提交回复
热议问题