我有一个简单的 jquery 投票系统,用户单击心形图标,值就会根据帖子当前的点赞数进行更新。这工作正常,但我想在另一个链接中显示更新的值。示例:
功能:
$(function() {
$(".fav").click(function() {
var id = $(this).attr("id");
var dataString = 'id='+ id ;
var parent = $(this);
$(this).fadeOut(300);
$.ajax({
type: "POST",
url: "/ajax/post_like.php",
data: dataString,
cache: false,
success: function(html) {
parent.html(html);
parent.fadeIn(300);
}
});
return false;
});
});
HTML 的样子:
<a href="#" class="post-reaction pull-right fav" id="{$p->pid}"><i class="fa fa-heart"></i></a>
<a href="#" class="post-reaction pull-right"><i class="fa fa-retweet"></i></a>
<a href="#" class="post-reaction notes pull-right">{$p->notes}</a>
我怎样才能实现这个目标?我还在学习 javascript 的过程中。任何帮助表示赞赏!
最佳答案
如果您只想将其增加 1,您可以将此添加到您的成功中:
var val = parseInt($(".notes").text());
$(".notes").text(val+1);
否则,如果 AJAX 返回的“html”是您想要使用的数字,只需替换
parent.html(html);
与
$(".notes").text(html);
更新:
根据您的评论,尝试以下操作:
parent.parent().find(".notes").text(html);
这将获取最接近的注释
(可能是您想要更新的注释)并更改其文本。
关于javascript - ajax 成功后,更改另一个链接值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26006379/