我有两个像这样的ajax函数:
$("body").on("click", ".follow", function() {
var followingId = $(this).data("following");
var followData = {"following_id" : followingId}
$(this).addClass('disabled');
$.ajax({
url: "/users/follow/",
type: "POST",
data: followData,
dataType: 'json',
context: this,
error: function () {},
success : function (response) {
$(this).addClass('unfollow');
$(this).attr('data-unfollow', response.row_id);
$(this).removeClass('follow');
}
});
});
$("body").on("click", ".unfollow", function() {
var unfollowId = $(this).data("unfollow");
var unfollowData = {"row_id" : unfollowId};
$(this).addClass('disabled');
$.ajax({
url: "/users/unfollow/",
type: "POST",
data: unfollowData,
context: this,
error: function () {},
success : function () {
$(this).removeClass('disabled');
$(this).addClass('follow');
$(this).removeClass('unfollow');
}
});
});
当我“关注”用户时,我得到“ row_id”(假设为row_id n°1)作为响应并将其设置为取消关注数据。如果我“取消关注”用户,则发送row_id n°1。这是正常的。
如果我再次尝试跟踪,则会得到一个新的row_id(例如,第2行)作为响应。那也是正常的。但是,如果我想再次取消关注,则发送row_id n°1!我想我应该更新
var unfollowId = $(this).data("unfollow");
(在html中,该值已更改为row_id n°2),但是当我发布row_id n°1时才发送。
我可以实现吗?
非常感谢 !
在第一个函数中,更改以下内容:
$(this).attr('data-unfollow', response.row_id);
对此:
$(this).data('unfollow', response.row_id);
仅供参考,您可以使用jQuery链接,如下所示:
$(this).removeClass('disabled');
$(this).addClass('follow');
$(this).removeClass('unfollow');
可以像这样更有效地完成:
$(this).removeClass('disabled').addClass('follow').removeClass('unfollow');
或者,如果您喜欢将它们放在单独的行中:
$(this).removeClass('disabled')
.addClass('follow')
.removeClass('unfollow');
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句