2017-04-06 48 views
0

此代码是为了避免在形式双击用的setTimeout禁用按钮几秒钟提交功能,避免双击不输入工作提交铬只

它运作良好,在IE &火狐但形式上绝不提交在Chrome

<input type="submit" id="button" class="button" onclick="foo(this);" name="submit_forgot" value="Submit" /> 

和这里的功能:

function foo(obj) { 
    obj.disabled = true; 
    setTimeout(function() { 
     obj.disabled = false; 
    }, 2000); 
    } 

感谢您的帮助!

回答

0

如果需要,我会用data来支持多个按钮。

$("#button").click(function() { 
 
    if ($(this).data("disable")) { 
 
    return false; 
 
    } 
 
    $(this).data("disable", true); 
 
    
 
    clearInterval($(this).data("interval")); 
 
    
 
    $(this).data("interval", setInterval(function() { 
 
    $(this).data("disable", false); 
 
    }.bind(this), 2000)); 
 
    
 
    console.log("called"); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="submit" id="button" class="button" name="submit_forgot" value="Submit" />

+0

非常感谢您!它运作良好!对于像我这样的其他newby,我不得不将它包装在$(document).ready(function(){}中。 – Vince