Jquery $(此选项)在使用引导时不使用单击

Jquery $(此选项)在使用引导时不使用单击,jquery,css,twitter-bootstrap,Jquery,Css,Twitter Bootstrap,我正在使用twitter引导程序中的图标。当用户发布状态更新时,我希望其他用户能够喜欢、不喜欢或喜欢该帖子。在他们喜欢这篇文章之后,我想改变图标的颜色,让他们知道他们按下了按钮。从其他堆栈溢出帖子中,我找到了不用jQuery实现这一点的方法。但是我想使用jQuery来完成它。目前,当我使用$(this).toggleClass(tst)添加我想要的颜色时,颜色不会改变。我知道我的ajax正在工作,因为数据库是根据我单击的特定帖子更新的 奇怪的是,如果我使用$(“#liked”).toggleCl

我正在使用twitter引导程序中的图标。当用户发布状态更新时,我希望其他用户能够喜欢、不喜欢或喜欢该帖子。在他们喜欢这篇文章之后,我想改变图标的颜色,让他们知道他们按下了按钮。从其他堆栈溢出帖子中,我找到了不用jQuery实现这一点的方法。但是我想使用jQuery来完成它。目前,当我使用
$(this).toggleClass(tst)
添加我想要的颜色时,颜色不会改变。我知道我的ajax正在工作,因为数据库是根据我单击的特定帖子更新的

奇怪的是,如果我使用$(“#liked”).toggleClass(tst),颜色确实会改变,但只有在使用#liked id的第一篇文章上才会改变。我尝试使用,toggleClass,addClass,removeClass/addClass.css,并在函数内外调用$(this).toggleClass(tst)。这一切都归结为一件事:当我使用
这个
时,它不会改变颜色。我如何(使用jQuery)以特定按钮为目标,以便更改其类

echo "<a id='liked' href='#' <span class='glyphicon glyphicon-thumbs-up'></span> </a>";
这是我的jQuery

$(".glyphicon").click(function() {

   var socialButton = $(this).attr('id');

   //traverse the dom up until I get to this post's text. Get the text from the post
        var thisPost = $(this).parent().parent().text();

   //get the user name of the post
   var user = $(this).parent().parent().parent().text();



    $.ajax({
        type: "POST",
        dataType: "json",
        url: "socialbuttons.php", 
    data: { 
          button: socialButton,
          post: thisPost,
          postAuthor: user
        },
        success: function(data) {
           $( this ).toggleClass("tst");
              alert("success")//this works
           //$("#liked").toggleClass("tst"); //works, but only on first post

        }
    })

    })

在成功回调中,您引用了错误的
this
;您可以通过
$.ajax()将其传递给

$.ajax({
类型:“POST”,
数据类型:“json”,

上下文:这,//在成功回调中,您引用了错误的
;您可以通过
$.ajax()传递它来修复它:

$.ajax({
类型:“POST”,
数据类型:“json”,

上下文:此,//将此添加到单击功能:

var icon = $(this);
而不是

$(this).toggleclass("tst");
使用


将此项添加到单击功能:

var icon = $(this);
而不是

$(this).toggleclass("tst");
使用


您需要保留该对象,因为在ajax success
$(this)
中只引用ajax(而不是当前单击的元素)


您需要保留该对象,因为在ajax success
$(this)
中只引用ajax(而不是当前单击的元素)

您在成功处理程序中使用了错误的“this”-它指向全局窗口对象(或者在本例中可能指向与ajax请求相关的某个对象,但无论如何)

另外,您可以编写
this.id
而不是
$(this.attr('id')
)。 哦,使用
.closest()
而不是级联
.parent()
s。或者更好的是,
$('.dunno类名')。在('click','.glyphicon',function(){…});
-在jQuery文档中查找
.on()

的第二个参数,您使用了错误的“this”在成功处理程序中-它指向全局窗口对象(或者在本例中可能指向与ajax请求相关的某个对象,但无论如何)

另外,您可以编写
this.id
而不是
$(this.attr('id')
)。
哦,使用
.closest()
而不是级联的
.parent()
s。或者更好的是,
$('.dunno类名')。on('click','.glyphicon',function(){…});
-在jQuery文档中查找
.on()的第二个参数

我看到你的代码生成了无效的HTML,也就是说,
你能更具体一点吗?先生,请更正你的HTML。。它不会生成有效的HTMLtag@K.K.Agarwalhtml有什么问题?我应该如何编写它?echo“”…您丢失了>我看到您的代码生成了无效的HTML,也就是说,
您能更具体一点吗?先生,请更正您的HTML..它不会生成有效的HTMLtag@K.K.Agarwalhtml有什么问题?我应该如何编写它?echo“”;…您丢失了>
icon.toggleClass("tst");`
$(".glyphicon").click(function () {
    var socialButton = $(this).attr('id');
    //traverse the dom up until I get to this post's text. Get the text from the post
    var thisPost = $(this).parent().parent().text();
    //get the user name of the post
    var user = $(this).parent().parent().parent().text();
    var obj = $(this);

    $.ajax({
        type: "POST",
        dataType: "json",
        url: "socialbuttons.php",
        data: {
            button: socialButton,
            post: thisPost,
            postAuthor: user
        },
        success: function (data) {
            obj.toggleClass("tst");
            alert("success") //this works
                //$("#liked").toggleClass("tst"); //works, but only on first post

        }
    })

})