jQuery的:捕捉锚的href的onclick并提交异步 [英] jQuery: Capture anchor href onclick and submit asynchronously

查看:190
本文介绍了jQuery的:捕捉锚的href的onclick并提交异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎从来没有得到与客户端的东西玩了,这presumably简单的任务是踢我的屁股:)

I almost never get to play with client-side stuff, and this presumably simple task is kicking my butt :)

我有一些联系。的OnClick我想prevent的默认操作,捕捉到它的HREF的URL,提交一个ajax GET到该网址,并简单地警报()的结果......但我甚至不能让过去的起跑线上:)

I have some links. OnClick I want to prevent the default action, capture its href url, submit an ajax GET to that url, and simply alert() with the results ... but I can't even get past the starting line :)

为例锚播放时间:

<a class="asynch_link" href="good/1.html">Click Here</a>
<a class="asynch_link" href="good/2.html">Click Here</a>

现在,我已经打了对的类似于的上提出要求一些建议,但链接仍然会导致浏览器导航到HREF URL。

Now, I've played with a few suggestions for similar requests on SO, but the links still cause the browser to navigate to the href url.

即使只有

<script type="text/javascript">
    $('a').click(function (event) 
    { 
         event.preventDefault(); 
         //here you can also do all sort of things 
    });
</script>

...的联系仍然导航到另一个页面。

...the links still navigate to the other page.

我还挺喜欢和婴儿在这里的感觉:)

I'm kinda feeling like and infant here :)

任何及所有的帮助会深深AP preciated。

Any and all help will be deeply appreciated.

和,是的,我的 AM 包括jQuery的:)
&LT;脚本的src =// 67.20.99.206/javascripts/jqueryCountdown/1-5-11/jquery.countdown.js类型=文/ JavaScript的字符集=utf-8&GT; &LT; / SCRIPT&GT;

And, yes, I AM including jQuery :)
<script src="//67.20.99.206/javascripts/jqueryCountdown/1-5-11/jquery.countdown.js" type="text/javascript" charset="utf-8"></script>

推荐答案

由于每个人所有帮助。
通过您的建议和反馈,我们现在有这个非常简单的解决方案。

Thanks to everyone for all the help.
Through your suggestions and feedback, we now have this perfectly simple solution.

$('a').click(function (event){ 
     event.preventDefault(); 
     $.ajax({
        url: $(this).attr('href')
        ,success: function(response) {
            alert(response)
        }
     })
     return false; //for good measure
});

......除非有人看到的东西,可以对此加以改进;)

...unless someone sees something that can be improved about it ;)

这篇关于jQuery的:捕捉锚的href的onclick并提交异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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