-1

我的代码看起来像这样。问题是,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;
});
4

1 回答 1

1

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

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;
            }
        }
    });

});
于 2012-09-08T12:29:41.590 回答