Ajax获取具有多个元素链接的元素的id [英] Ajax get id of element where there are multiple links with elements
本文介绍了Ajax获取具有多个元素链接的元素的id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下代码是我当前用来尝试将帖子ID提供给vote.php的代码,但是当前返回[object Object].点击链接后,如何传递正确的ID?
The following code is what I am currently using to try and give the posts ID to vote.php, however this currently returns [object Object]. How would I be able to pass the correct id when a link is clicked?
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
$.ajax({
type: "POST",
data: "id=" + $(this).attr("href", "id"),
url: "vote.php"
});
</script>
<a href="javascript:;" id="1"><div id="button">Like!</div></a>
<a href="javascript:;" id="2"><div id="button">Like!</div></a>
<a href="javascript:;" id="3"><div id="button">Like!</div></a>
谢谢!
推荐答案
您需要将ajax调用绑定到点击处理程序:
You need to bind the ajax call to a click handler:
$(document).on("click","a",function(e){
$.ajax({
type: "POST",
data: "id=" + $(this).attr("id"),
url: "vote.php"
});
});
这篇关于Ajax获取具有多个元素链接的元素的id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文