简体   繁体   English

如何防止按钮重复提交

[英]How to prevent double submission of the button

I have two buttons, one button for increasing a count and other button to submit the count and move to next page.我有两个按钮,一个按钮用于增加计数,另一个按钮用于提交计数并移至下一页。 But the error I face is that when I press the button "Proceed", it needs to be pressed twice to move to the next page and also the count is doubled and submitted.但我面临的错误是,当我按下“继续”按钮时,需要按下两次才能移动到下一页,并且计数加倍并提交。 The following is the code:以下是代码:

Code:代码:

  <button class="btn_success" id="btn_add" value="add areas">Confirm</button>

  <button class="brown-button" id="Proceed" onclick="location.href='gallery.php'; return false;">Proceed &nbsp;😀</a></button>

    

    var imagecount=0;

    $('.btn_success').click(function(){
      imagecount+=1;
      
      });


    $("#Proceed").click(function(){

        $.ajax({
                type:'POST',
                url: 'server.php',
                data:{
                    'imagecount':imagecount,
                      },
          success: function(data){
                alert(imagecount);
                }
                })
          });

For the above code, the btn_success is used to increment the count and when I press the Proceed button, the count value is submitted and it moves to next page.对于上面的代码,btn_success 用于增加计数,当我按下继续按钮时,计数值被提交并移动到下一页。 But, when Proceed button is pressed , the alert pops up twice and the count value submitted is doubled ie, if the count is 3 when I press Proceed button, it is saved as 6. I want to recitfy this error and save it as value 3.但是,当按下 Proceed 按钮时,警报弹出两次并且提交的计数值加倍,即,如果按下 Proceed 按钮时计数为 3,则将其保存为 6。我想重新引用此错误并将其保存为值3.

Can someone help me solve this problem有人可以帮我解决这个问题吗

In simple terms, in your event handler (which is missing, for id="finish" there's no element), you can do something like this:简单来说,在您的事件处理程序中(缺少,因为id="finish"没有元素),您可以执行以下操作:

 $(function () { $("#finish").click(function () { // Cache this to use inside another function. $this = $(this); $this.prop("disabled", true); console.log("Sending AJAX call... You can't re-submit the Finish button again... Please wait..."); setTimeout(function () { console.log("Got output from server..."); $this.prop("disabled", false); }, 1000); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button id="finish">Finish</button>

For a better UX, you can do something like this:为了获得更好的用户体验,您可以执行以下操作:

 $(function () { $("#finish").click(function () { // Cache this to use inside another function. $this = $(this); $this.prop("disabled", true); $("#notification").text("Sending AJAX call... You can't re-submit the Finish button again... Please wait..."); setTimeout(function () { $("#notification").text("Got output from server..."); $this.prop("disabled", false); }, 1000); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button id="finish">Finish</button> <span id="notification"></span>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM