js 防止重復提交


1、用flag標識,下面的代碼設置submited 標志

/*方法1:設置一個全局JS變量*/
        var submited = false;
        function checkSubmit() {
            if (!submited)
            {
                submited = true;
                return true;
            }
            else {
                alert("請不要重復提交!");
                return false;
            }
        }
 
        function submitForm() {
            var f = document.getElementById("inputForm");
            if (checkSubmit())
            {
                f.submit();
            }
        }

2、在onsubmit事件中設置,在第一次提交后使提交按鈕失效

<form action=”about:blank” method=”post” onsubmit =”getElementById(‘submitInput').disabled=true;return true;” target=”_blank”> 
<input type=”submit” id=”submitInput”/> 
</form> 
</body> 
</html> 
</script> 

3、今天發現jquery本身就可以很好的實現

$.ajax({
//$.ajax請求中的beforeSend方法中把提交按鈕禁用掉 beforeSend: function(){ $("#submit").attr({ disabled: "disabled" }); }, complete: function(){
//Ajax請求執行完畢,在恢復按鈕的可用狀態。 $("#submit").removeAttr("disabled"); } // ......

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM