2012-11-13 45 views

回答

3

這可以幫助你:http://jsfiddle.net/jhNcM/

<input type="button" id="aaa" value="button" /> 

$('#aaa').click(function() { 
    var aaa = $(this); 
    aaa.prop('disabled', true); 
    setTimeout(function() { 
     aaa.prop('disabled', false); 
    }, 3000); 
}); 
0
$("#button").click(function() { 
    if (this.disabled) { 
     return; 
    } 
    this.disabled = true; 
    setTimeout($.proxy(function() { 
     this.disabled = false; 
    }, this), 3000); 
}); 
+0

如果你已經使用jQuery(代理),爲什麼不使用$()。prop()方法? – Naor

+0

@Naor因爲它更短並且不使用字符串:'this.disabled = true' vs'$(this).prop(「disabled」,true)' – Esailija

1

有一種方法可以做到這一點http://jsfiddle.net/Ktk6f/

HTML

<input type="submit" value="submitData" id="myButton" /> 

JS

$('#myButton').click(function(){ 
    var that = $(this); 
    that.attr('disabled', true); 
    var timer = setTimeout(function(){ 
     that.attr('disabled', false); 
    }, 1000); 
}); 

它需要jQuery的JS框架

+0

@Kessi:最好使用prop來代替attr: http://stackoverflow.com/questions/5874652/prop-vs-attr – Naor

0

普通的JavaScript:http://jsfiddle.net/R5p5q/1/

<form id="myForm"> 
    <input id="mySubmit" type="submit" value="GO" /> 
</form> 

var myForm = document.getElementById('myForm'); 

myForm.addEventListener("submit", function(evt) { 
    var elemSubmit = document.getElementById('mySubmit'); 
    elemSubmit.setAttribute("disabled", "disabled"); 

    // Removes disabling after 3 seconds 
    window.setTimeout(function() { 
     elemSubmit.removeAttribute("disabled"); 
    }, 3e3); 
},false);