Illegal invocation Error

守給你的承諾、 提交于 2019-12-12 03:16:29

问题


I only have one function in my scripts page, and it is giving me this error: Uncaught TypeError: Illegal invocation. To be honest, I've never seen this error before, and none of the other cases that I found online seemed to apply to me. My jquery is below, and I don't think any other pieces are necessary, but let me know and I can post other parts.

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

回答1:


The problem is in your $.post call. You're trying to set 'delivered' to delivered, which is a jQuery object, I assume you meant deliveredBoolean.

Also, in the callback function this is not what you think it is, it's the jqXHR object, not the element.

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



回答2:


I assume the error is inside of this part:

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

Here I think you want this to be the same as above when you are using closest to get the "tr" element. But in here this is the context of the $.post imho.

You either need to bind or rather do var boxChange = $(this), at the top of the event handler function and use the cached reference afterwards



来源:https://stackoverflow.com/questions/10232811/illegal-invocation-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!