jQuery的:捕捉锚的href的onclick并提交异步jQuery、href、onclick

2023-09-10 16:06:47 作者:别再互相折磨

我几乎从来没有得到与客户端的东西玩了,这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 ;)