jQuery:捕获锚点href onclick并异步提交 [英] jQuery: Capture anchor href onclick and submit asynchronously
问题描述
我几乎从来没有玩过客户端的东西,这个大概很简单的任务就是踢我的屁股:)
I almost never get to play with client-side stuff, and this presumably simple task is kicking my butt :)
我有一些链接.OnClick 我想阻止默认操作,捕获它的 href url,向该 url 提交一个 ajax GET,然后简单地 alert()
和结果......但我什至无法通过开始行:)
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>
现在,我已经针对 SO 上的类似请求提出了一些建议,但这些链接仍然会导致浏览器导航到 href 网址.
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 :)
我们将不胜感激任何和所有帮助.
Any and all help will be deeply appreciated.
而且,是的,我 AM 包括 jQuery :)<script src="//67.20.99.206/javascripts/jqueryCountdown/1-5-11/jquery.countdown.js" type="text/javascript" charset="utf-8"></脚本>
推荐答案
$('a').click(function(event) {
event.preventDefault();
$.ajax({
url: $(this).attr('href'),
success: function(response) {
alert(response);
}
});
return false; // for good measure
});
这篇关于jQuery:捕获锚点href onclick并异步提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!