收录日期:2019/05/20 13:10:18 时间:2012-08-30 01:00:53 标签:javascript,dom

How would I do this without jQuery?

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

It doesn't have to be cross-browser compatible; a solution that only works in Firefox is OK.

var inputs = document.getElementsByTagName("INPUT");
for (var i = 0; i < inputs.length; i++) {
    if (inputs[i].type === 'submit') {
        inputs[i].disabled = true;
    }
}

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

    }
}

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';
  }
}
var els = document.getElementsByTagName ( 'input' );
for ( var i = 0; i < els.length ; i ++ ) {
 if ( els[i].type == 'submit' ) els[i].setAttribute('disabled', 'true'); 
}