Ajax不执行成功函数

时间:2012-09-08 11:26:24

标签: javascript php jquery ajax

我的代码看起来像这样。问题是,PHP方面做的工作并返回正确的价值。但是ajax不执行success: function内的事情。我错过了什么?

AnswerDiv.on("click", ".NotSelectedAnswer", function() {
    var NotSelectedAnswerBtn = $(".NotSelectedAnswer"),
        SelectedAnswerBtn = $(".SelectedAnswer"),
        AnswerDiv = $("div.Answer"),
        querystring="fromID="+SelectedAnswerBtn.data("id")+"&toID="+$(this).data("id")+"&op=SelectAsAnswer";

    $.ajax({
        url: 'processor.php',
        type: "POST",
        dataType: "json",
        data: querystring,
        success: function(data) {
            if(data.status)
            {
                SelectedAnswerBtn.removeClass("SelectedAnswer").addClass("NotSelectedAnswer").button("enable");
                $(this).removeClass(" NotSelectedAnswer").addClass("SelectedAnswer").button("disable");
                $("div.Answer[data-id=" + SelectedAnswerBtn.data("id") + "]").toggleClass("SelectedDiv");
                $("div.Answer[data-id=" + $(this).data("id") + "]").toggleClass("SelectedDiv");
            }
        }
    });

    return false;
});

1 个答案:

答案 0 :(得分:1)

尝试在ajax调用之前缓存$(this)

AnswerDiv.on("click", ".NotSelectedAnswer", function() {
    var NotSelectedAnswerBtn = $(".NotSelectedAnswer"),
    SelectedAnswerBtn = $(".SelectedAnswer"),
    AnswerDiv = $("div.Answer"),
    thisElem=$(this),
    querystring="fromID="+SelectedAnswerBtn.data("id")+"&toID="+$(this).data("id")+"&op=SelectAsAnswer";
    $.ajax({
        url: 'processor.php',
        type: "POST",
        dataType: "json",
        data: querystring,
        success: function(data) {
            if(data.status)
            {
                SelectedAnswerBtn.removeClass("SelectedAnswer").addClass("NotSelectedAnswer").button("enable");
                thisElem.removeClass(" NotSelectedAnswer").addClass("SelectedAnswer").button("disable");
                $("div.Answer[data-id=" + SelectedAnswerBtn.data("id") + "]").toggleClass("SelectedDiv");
                $("div.Answer[data-id=" +thisElem.data("id")+ "]").toggleClass("SelectedDiv");
                return false;
            }
        }
    });

});