weixin_33711641 2015-03-23 12:57 采纳率: 0%
浏览 27

单击.class按钮

i have a div with multiple like buttons and i have the following code where when a user press on one of the like buttons it returns the data-catid value.

$('.likes').click(function(){
            var catid;
            catid = $(this).attr("data-catid");
             $.get('/rmb/like_image/', {like_id: catid}, function(data){
                       $('.likes').html(data);
                       $('.likes').attr('disabled','disabled');
                   });
        });

My problem is instead of returning it to the only button that was pressed , its returning the value to all buttons. I know it has something to do with this line

$('.likes').html(data);

but i am not sure how to fix it.

Any suggestions? Thanks

  • 写回答

3条回答 默认 最新

  • elliott.david 2015-03-23 12:59
    关注

    You need to update the current clicked element, so use a closure variable to store the reference to the clicked element then use that reference on the ajax callback.

    $('.likes').click(function () {
        var $this = $(this),
            catid = $this.attr("data-catid");
        $.get('/rmb/like_image/', {
            like_id: catid
        }, function (data) {
            $this.html(data).prop('disabled', true);
        });
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗