非法调用错误

时间:2012-04-19 16:44:38

标签: javascript jquery post live

我的脚本页面中只有一个函数,它给了我这个错误:Uncaught TypeError:非法调用。说实话,我以前从未见过这个错误,我在网上找到的其他案例似乎都没有适用于我。我的jquery在下面,我认为不需要任何其他部分,但请告诉我,我可以发布其他部分。

$(document).ready(function () {
    /*----UPDATE BOX REQUEST----*/
    $(".boxesChange").live("click", function () {
        entry = $(this).closest("tr");
        delivered = $(entry).find("#delivered");
        if ((delivered).is(":checked")) {
            deliveredBoolean = "1";
        } else {
            deliveredBoolean = "0";
        }
        boxesDelivered = $(entry).find("#boxesDelivered").val();
        bubbleWrapDelivered = $(entry).find("#bubbleWrapDelivered").val();
        supplyRequestId = $(entry).find(".boxesSupplyRequestId").val();

        $.post('boxesChange.php', {
            'delivered': delivered,
            'boxesDelivered': boxesDelivered,
            'bubbleWrapDelivered': bubbleWrapDelivered,
            'supplyRequestId': supplyRequestId
        }, function (response) {
            $(this).closest(".boxesScheduleEntry").css("background-color", "#ccffcc");
        });
        return false;
    });
});

2 个答案:

答案 0 :(得分:1)

我认为错误在这部分内部:

function (response) {
  $(this).closest(".boxesScheduleEntry").css("background-color", "#ccffcc");
}

在这里,我认为当你使用最接近的" tr"时,你希望this与上面相同。元件。但在这里this是$ .post imho的上下文。

您需要绑定或者更确切地在事件处理函数的顶部执行var boxChange = $(this),并在之后使用缓存的引用

答案 1 :(得分:1)

问题出在您的$.post电话中。您正尝试将'delivered'设置为delivered,这是一个jQuery对象,我假设您的意思是deliveredBoolean

另外,在回调函数中this不是你想象的那样,它是jqXHR对象,而不是元素。

var $this = $(this);
$.post(
        'boxesChange.php', 
        {
            'delivered': deliveredBoolean,
            'boxesDelivered': boxesDelivered,
            'bubbleWrapDelivered': bubbleWrapDelivered,
            'supplyRequestId': supplyRequestId
        },
        function (response) {
            $this.closest(".boxesScheduleEntry").css("background-color", "#ccffcc");
        }
);