脚本宝典收集整理的这篇文章主要介绍了php – jQuery AJAX在成功函数中引用$(this),脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
这是jquery:
$(".Vote_up").click(function(){ VAR id = this.id; var Vote = $(this).attr("class"); var data = "id=" + id + "&Vote=" + Vote; $.ajax ({ tyPE: "POST",url: "Vote.PHP",data: data,cache: false,success: function(data) { for(var x in data) { $(".Votes_up").find(id).htML(data[x].Vote_up); $(".Votes_down").find(id).html(data[x].Vote_down); } } }); });
因此,当我首先构建项目时,我将数据库中的记录ID设置为项目ID.所以我要做的是引用被点击的确切项目,并将其HTML设置为从PHP返回的数据.我已经检查了Firebug并且我得到了正确的数据,但投票数没有变化.有任何想法吗?
这是PHP供参考:
$query = "SELECT Vote_up,Vote_down From posts WHERE id = '".$id."'"; $result1 = MysqL_query($query); $output = Array(); while ($row = MysqL_fetch_array($result1)){ $output[] = Array( "Vote_up" => $row['Vote_up'],"Vote_down" => $row['Vote_down'],); } echo json_encode($output);
$.ajax({ context: this,// set the context of the callbacks type: "POST",success: function(data) { // Now "this" refers to the element that was clicked }
您可以通过执行更通用的操作来测试它,例如:
$(this).html("yep,IT works");
…然后,如果有效,请考虑在循环中的同一元素上执行.html()实际上没有意义,因为每次.html()都会覆盖整个内容.
如果要从循环中追加数据,请使用.append():
for(var x in data) { $(this).append(data[x].Vote_up); $(this).append(data[x].Vote_down); }
以上是脚本宝典为你收集整理的php – jQuery AJAX在成功函数中引用$(this)全部内容,希望文章能够帮你解决php – jQuery AJAX在成功函数中引用$(this)所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。