JQuery的:一个链接之后才执行后 [英] JQuery: Perform a post before a link is followed

查看:113
本文介绍了JQuery的:一个链接之后才执行后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做一个异步张贴到服务器时,有人在我的网站点击一个链接。我试过code以下,但它似乎没有做我希望它在服务器上的任务。我认为问题是,它是下面的内容可能会发生之前的链接。我实际上并不需要的响应等待从服务器返回的,我只需要它来调用发送给它。任何人都可以帮我吗?也许如何延缓下面的链接它被调用之前或者我应该注意使用$。员额?

  $(#项目)。点击(函数(EV){
    $。员额(link_to_something);
});

&其中;一的id =项目的href =www.google.com>连结&所述; / a取代;
 

解决方案

的第一件事是通过返回从点击处理虚假取消链接的默认操作。这将有足够的时间为AJAX请求完成。它完成时,成功的回调里面,你可以重定向到任何地方的链接指向:

  $(#项目)。点击(函数(EV){
    无功自我=这一点;
    $。员额(link_to_something,函数(){
        window.location.href = self.href;
    });
    返回false;
});
 

I need to do an asynchronous post to a server when someone on my site clicks a link. I tried the code below, but it doesn't seem to be doing the task on the server I want it to. I think the problem is that it is following the link before something can happen. I don't actually need to wait for the response back from the server, I just need it to send the call to it. Can anyone help me out? Maybe how to delay following the link before it is called or should I be note using $.post?

$("#item").click(function (ev) {
    $.post(link_to_something);
});

<a id="item" href="www.google.com">Link</a>

解决方案

The first thing is to cancel the default action of the link by returning false from the click handler. This will allow enough time for the AJAX request to complete. And when it completes, inside the success callback you could redirect to wherever the link is pointing to:

$("#item").click(function (ev) {
    var self = this;
    $.post(link_to_something, function() {
        window.location.href = self.href;
    });
    return false;
});

这篇关于JQuery的:一个链接之后才执行后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆