你好,我做了一个代码,要求在ajax调用后添加一个类。我确定代码正确,但是仍然没有添加该类。它真的很奇怪,因为代码中的其他所有内容都可以正常工作,而且我确定addClass的代码正确,而且我已经检查了控制台是否有任何错误,但是没有错误。这是我的代码

$(document).on('click', '.miclickks', function(event) {
 event.preventDefault();

var for_uid    = $(this).parents("li").attr('data');
var for_name   = $(this).parents("li").attr('unme');
var for_pic    = $(this).parents("li").attr('upic');
var owner_uid  = $('.row-fluid').attr('uid');
var owner_name = $('.row-fluid').attr('usnm');
var owner_pic  = $('.row-fluid').attr('usp');
var type       = "kiss";

var dataString = "type=" + type + "&for_uid=" + for_uid + "&for_name=" + for_name + "&for_pic=" + for_pic + "&owner_uid=" + owner_uid + "&owner_pic=" + owner_pic + "&owner_name=" + owner_name;

        $.ajax({
            type: "POST",
            url: "include/ajax.php",
            data: dataString,
            success: function (html) {
            if(html=="300")
            {
            $('#myModal .modal-body p').html("Error Please Try Again.");

            $('#myModal').modal('show');
            }
            else
            {
            $(this).addClass('active');


            }
            }
        });



});

最佳答案

两种方式:

第一:

var that = this;
$.ajax({
    type: "POST",
    url: "include/ajax.php",
    data: dataString,
    success: function (html) {
        if (html == "300") {
            $('#myModal .modal-body p').html("Error Please Try Again.");
            $('#myModal').modal('show');
        } else {
            $(that).addClass('active');
        }
    }
});


第二(使用context选项):

$.ajax({
    type: "POST",
    url: "include/ajax.php",
    data: dataString,
    context: this,
    success: function (html) {
        if (html == "300") {
            $('#myModal .modal-body p').html("Error Please Try Again.");
            $('#myModal').modal('show');
        } else {
            $(this).addClass('active');
        }
    }
});

09-30 16:09