1

所以我发送一个ajax请求如下

$('a.new_thumb_update').click(function(e)
{
    e.preventDefault();
    share_id = $(this).attr('share_id');

    $.post('http://studeez.net/resource_layout_files/includes/thumbs_up.php',
    {share_id:share_id},
    function(data)
    {
        alert(data);
        $(this).parent().parent().find('div.thumbs_up').hide();
    });
});

代码功能以及我获得警报反馈的回调函数。

$(this).parent().parent().find('div.thumbs_up').hide();

如果我用实际元素替换 $(this),它就可以工作。如下所示

$('a.new_thumb_update').parent().parent().find('div.thumbs_up').hide();
4

3 回答 3

5

你需要在$(this)这里缓存:

$('a.new_thumb_update').click(function (e) {
    e.preventDefault();
    var $self = $(this);
    share_id = $self.attr('share_id');

    $.post('http://studeez.net/resource_layout_files/includes/thumbs_up.php', {
        share_id: share_id
    }, function (data) {
        alert(data);
        $self.parent().parent().find('div.thumbs_up').hide();
    });
});

as在 jQuery ajax方法$(this)中不可访问。$.post

于 2013-10-28T12:22:24.340 回答
0

尝试这个,

$('a.new_thumb_update').click(function(e){
    e.preventDefault();
    share_id = $(this).attr('share_id');
    var self=this;// assign this to self and use it
    $.post('http://studeez.net/resource_layout_files/includes/thumbs_up.php',
      {share_id:share_id},
      function(data){
        alert(data);
        $(self).parent().parent().find('div.thumbs_up').hide();// use self instead of this
    });
});
于 2013-10-28T12:21:53.390 回答
0
$('a.new_thumb_update').click(function(e)
{
    e.preventDefault();
    var _this=$(this);// set this in here
    share_id = _this.attr('share_id');

    $.post('http://studeez.net/resource_layout_files/includes/thumbs_up.php',
    {share_id:share_id},
    function(data)
    {
        alert(data);
        _this.parent().parent().find('div.thumbs_up').hide();
    });
});
于 2013-10-28T12:22:14.217 回答