jQuery Ajax发送两次第二次提交和三次第三次提交

时间:2022-11-23 20:26:59

I have a php file "popup_or_abbort.php" If the user have a request already, then the request will be abborted and the row in the db will be deleted with mysql_query. If I go on the page for the first time and send a request, it sends the request once. If I submit a second time it abborts the request. If I send a request again, without updating the page, it sends the request twice. Please help me to solve this problem.

我有一个php文件“popup_or_abbort.php”如果用户已经有请求,那么请求将被取消,db中的行将被mysql_query删除。如果我第一次进入页面并发送请求,它会发送一次请求。如果我第二次提交,它会拒绝请求。如果我再次发送请求而不更新页面,它会发送请求两次。请帮我解决这个问题。

$(document).ready(function(){

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

      uid = "<?php echo $user_id; ?>";        
      pid = "<?php echo $p_id; ?>";           

      $.ajax ({
        type: "POST",
        url: "inc/scripts/popup_or_abbort.php",
        data: {"uid" : uid, "pid" : pid},
        success: function(data){
          if (data == "send_ok") {            

            $('#popup').fadeIn(300);
            $('#sendbutton').click(function(){
              pn = "<?php echo $p_name; ?>";
              un = "<?php echo $username; ?>";
              oid = "<?php echo $p_owner_id; ?>";
              app = $('#application').val();

              $.ajax({
                type: "POST",
                url: "inc/scripts/send_team_request.php",
                data: {"uid" : uid, "pid" : pid, "pn" : pn, "un" : un, "oid" : oid, "app" : app},
                success: function(data){
                  $('#request').html(data);
                  $('#application').val("");
                  $('#popup').fadeOut(300);

                  return false;                      
                }
              });                 
            });
          }
          else {
            $('#request').html(data);
            return false;
          }  
        }          
      }); 
  return false;              
  }); 
return false;           
});

1 个解决方案

#1


4  

You are assigning $('#sendbutton').click(function(){ handler inside ajax callback. It actually get's assigned new (identical) handler each time ajax callback is executed. So, upon #sendbutton click - multiple identical ajax requests are being executed.

你正在分配$('#sendbutton')。点击(function(){ajax回调里面的处理程序。每次执行ajax回调时,它实际上都被分配了新的(相同的)处理程序。所以,在#sendbutton点击时 - 多个相同的ajax请求是被执行。

Instead, delegate it once outside of ajax call:

相反,在ajax调用之外委托它一次:

$(document).on("click", "#sendbutton", function() {
    ...
});

#1


4  

You are assigning $('#sendbutton').click(function(){ handler inside ajax callback. It actually get's assigned new (identical) handler each time ajax callback is executed. So, upon #sendbutton click - multiple identical ajax requests are being executed.

你正在分配$('#sendbutton')。点击(function(){ajax回调里面的处理程序。每次执行ajax回调时,它实际上都被分配了新的(相同的)处理程序。所以,在#sendbutton点击时 - 多个相同的ajax请求是被执行。

Instead, delegate it once outside of ajax call:

相反,在ajax调用之外委托它一次:

$(document).on("click", "#sendbutton", function() {
    ...
});