weixin_33743880 2015-12-03 19:51 采纳率: 0%
浏览 207

jQuery-选择器不起作用

I want to add like functionality to my site using jQuery but my element selectors aren't working.

HTML:

<a class="btn-like" id="1">2</a>

JQUERY(HTML NOT WORKING):

$( document ).ready(function() {
  $(".btn-like").click(function() {
    var like_id = $(this).attr("id");
    $('a.btn-like#'+like_id).html('Loading ...');
    $.ajax({
      type: "POST",
      url: "like.php",
      data: 'item_id='+like_id,
      cache: false,
      success: function(data) {
        $('a.btn-like#'+like_id).html(data);
        alert(data); //correct response
        $('a.btn-like#'+like_id).addClass('liked');
      }
    });
    return false;
  });
});

JQUERY(HTML WORKING):

$( document ).ready(function() {
  $(".btn-delete").click(function() {
    var item_id = $(this).attr("id");
    $('a.btn-delete#'+item_id).html('Loading ...');
    $.ajax({
      type: "POST",
      url: "post.php",
      data: 'item_id='+item_id,
      cache: false,
      success: function(data) {
        $('div#post_'+item_id).remove();
      }
    });
    return false;
  });
});

The response of the ajax post is correct. But the .html update is not working

The content is not changed after clicking the link.

  • 写回答

1条回答 默认 最新

  • weixin_33696106 2015-12-03 19:55
    关注

    For an anchor tag, you should be updating the text property. $('a.btn-like#'+like_id).text(data);

    评论

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法