JQuery:当用户点击链接时,首先提交表单然后转到链接

时间:2022-12-01 10:51:06

SO I have a setup with a few links as follows

所以我有一个如下设置的链接

<a class="someclass" href="http://www.google.com" id="link01">Google</a>
<a class="someclass" href="http://www.yahoo.com" id="link02">Yahoo</a>
<a class="someclass" href="http://www.cnn.com" id="link03">CNN</a>
<a class="someclass" href="http://www.facebook.com" id="link04">Facebook</a>

$(".someclass").click(function() {
            var currentId = $(this).attr('id');
            var data = "do=clearthis&id=" + currentId;

              $.ajax({
                 type: "POST",
                  url: "clearance.php",
                  data: data,
                  success: function(msg){
                   }
                 });
               });
});

What I am doing is tking the link that is clicked, obtaining an id value from the link and ajax submitting that to my PHP file which will remove the link from my database.

我正在做的是点击被点击的链接,从链接获取一个id值,并将ajax提交给我的PHP文件,这将从我的数据库中删除链接。

So that when a user clicks on a link, that link is removed from my database. However, the problem is that its not actually working. I htink the problem is that its too fast, as jquery begins to POST that form for me, its already jumping to the target link. Is there any way to induce some kind of delay to allow my clearance.php to process the removal before allowing the user to continue to the target link?

因此,当用户单击链接时,该链接将从我的数据库中删除。但问题是它实际上并没有起作用。我想问的是它的速度太快了,因为jquery开始为我发布POST表单,它已经跳转到目标链接。有没有办法诱导某种延迟,允许我的clearance.php在允许用户继续目标链接之前处理删除?

THanks!

2 个解决方案

#1


3  

Make a synchronous AJAX request so that it completes first:

进行同步AJAX请求,使其首先完成:

$.ajax({
  type: "POST",
  async: false,
  url: "clearance.php",
  data: data,
  success: function(msg){
  }
});

#2


1  

Another solution without making the request synchronous:

另一个解决方案没有使请求同步:

$(".someclass").click(function() {
    var currentId = $(this).attr('id');
    var data = "do=clearthis&id=" + currentId;
    var linkUrl =   $(this).attr("href");
        $.ajax({
             type: "POST",
                url: "clearance.php",
                data: data,
                success: function(msg){
                 },
                complete: function(){
                    window.location.href = linkUrl;
                }
             });
         });
    return false;
});

#1


3  

Make a synchronous AJAX request so that it completes first:

进行同步AJAX请求,使其首先完成:

$.ajax({
  type: "POST",
  async: false,
  url: "clearance.php",
  data: data,
  success: function(msg){
  }
});

#2


1  

Another solution without making the request synchronous:

另一个解决方案没有使请求同步:

$(".someclass").click(function() {
    var currentId = $(this).attr('id');
    var data = "do=clearthis&id=" + currentId;
    var linkUrl =   $(this).attr("href");
        $.ajax({
             type: "POST",
                url: "clearance.php",
                data: data,
                success: function(msg){
                 },
                complete: function(){
                    window.location.href = linkUrl;
                }
             });
         });
    return false;
});