我想在提交按钮上设置延迟。换句话说,点击该按钮后灰色显示几秒钟。我怎样才能用js完成这个?
答案 0 :(得分:4)
这可能会对您有所帮助: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);
});
答案 1 :(得分: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框架
答案 2 :(得分:1)
普通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);
答案 3 :(得分:0)
$("#button").click(function() {
if (this.disabled) {
return;
}
this.disabled = true;
setTimeout($.proxy(function() {
this.disabled = false;
}, this), 3000);
});