2011-11-04 114 views
0

即时创建一个类似的系统,但我已经遇到了一个问题,当用户按下像“喜欢”的文本应更改为“删除像”jQuery的替换文本内锚

数据来源于一个PHP查询所以那里有与同级别的许多项目

<table class="UserHistoryTable"> 
<td data-rowtype="stat" data-rowuser="1" data-rowid="1" class="UserHistoryTableTD"> 
<a href="#" class="like">Like</a></td> 
</table> 

数据rowuser是用户按下喜欢按钮的ID,数据ROWID是活动 的什么我想要做的是id,当有人按下“像“链接,它应该更改为”删除像“ 我已尝试使用此代码:

$('.like').click(function(){ 
var the_id = $(this).parent().data('rowid') 
var user_id = $(this).parent().data('rowuser') 
var user_id = $(this).parent().data('rowtype') 
    $.ajax({ 
     url: "like.php", 
     data: "action=likestat" + "&id=" + the_id + "&uid=" + user_id, 
     success:function(response){ 
      var findtext = $('.UserHistoryTableTD > .like', this); 
      $(findtext).replaceWith("Remove like"); 
     } 
    }); 
}); 

回答

3

你有几个问题。 this在成功处理程序中的值不会与您想要的值相同(它将具有与ajax调用相关的不同值)。而>选择器意味着直接子和.like不是父表的直接子。如果将this的值保存在局部变量中,则可以直接在ajax处理程序中使用它。

更改代码这样:

$('.like').click(function(){ 
    var the_id = $(this).parent().data('rowid') 
    var user_id = $(this).parent().data('rowuser') 
    var self = this; // save for use in success handler 
    $.ajax({ 
     url: "like.php", 
     data: "action=likestat" + "&id=" + the_id + "&uid=" + user_id, 
     success:function(response){ 
      $(self).html("Remove like"); 
     } 
    }); 
}); 

你也可以干它有点儿像这样:

$('.like').click(function(){ 
    var $self = $(this);    // save this for use later 
    var $td = $self.closest("td"); // find correct parent, regardless of other markup 
    $.ajax({ 
     url: "like.php", 
     data: "action=likestat" + "&id=" + $td.data('rowid') + "&uid=" + $td.data('rowuser'), 
     success:function(response){ 
      $self.html("Remove like"); 
     } 
    }); 
}); 
+0

为什么'$ self'? (只想学习) – samura

+1

@samura - 您必须将'this'的值保存在局部变量中,以便您可以在成功处理程序中使用它(因为'this'将在成功处理程序中具有不同的值)。由于我们需要'$(this)'在几个地方,我保存它而不是'this'。一个受保护的'this'变量的流行约定是'self'。保存jQuery对象的另一个流行约定是以'$'开头。因此,我们最终以'$ self'作为变量名称(一个表示'this'的jQuery对象)。 – jfriend00

+0

哇,这比我问的还要多!感谢您缩短我的代码,并帮助我:)(我喜欢当人们帮助我) – Flaashing

0

你应该只能够做到:

$(this).html("Remove like"); 

你的点击事件处理中。

+0

以及ID喜欢肯定的是,像得到了插入数据库编辑文本之前,但该选项的工作,另一方面:)谢谢 – Flaashing

+0

'this'在ajax成功处理程序中没有正确的值。 – jfriend00

1

不能使用this内的成功,让您保存一个变种$(this)您的成功事件之外。

$('.like').click(function(){ 
var like_button = $(this); 
var the_id = $(this).parent().data('rowid'); 
var user_id = $(this).parent().data('rowuser'); 
var user_id = $(this).parent().data('rowtype'); // something is wrong here :s 
    $.ajax({ 
     url: "like.php", 
     data: "action=likestat" + "&id=" + the_id + "&uid=" + user_id, 
     success:function(response){ 
      like_button.replaceWith("Remove like"); 
     } 
    }); 
}); 

(编辑詹姆斯评论的建议)

而且什么是错的行数4/5。

+0

由于您已经在第一行将'this'传入jQuery,因此无需在'success'函数中再次执行。但+1是第一个正确回答的! –

+0

你是对的...编辑。 – samura

+0

狗屎谢谢你指出我的失败..我不明白为什么那部分没有工作:/谢谢 并感谢一吨的答案!它美丽的工作:) – Flaashing

0

使用jQuery的.html()函数可以更轻松地完成。
http://jsfiddle.net/Hz7WY/

头:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> 
<script type="text/javascript"> 
$('.like').click(function(){ 
var the_id = $(this).parent().data('rowid') 
var user_id = $(this).parent().data('rowuser') 
var user_id = $(this).parent().data('rowtype') 
     $('.like').html('Unlike'); 
}); 
</script> 

身体:

<table class="UserHistoryTable"> 
<td data-rowtype="stat" data-rowuser="1" data-rowid="1" class="UserHistoryTableTD"> 
<a href="#" class="like">Like</a></td> 
</table>