2012-04-19 30 views
0

我的腳本頁面只有一個函數,它給了我這個錯誤: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

當'deliver'是一個jQuery對象時,使用''deliver':delivered'是否奇怪?也許你應該做'deliver:deliver.is(「:checked」)? 1:0'或使用你的'deliverBoolean'的東西? – 2012-04-19 16:57:33

+0

@jamund我不確定我是否明白你在那裏放置那個 – radleybobins 2012-04-19 17:03:48

+1

@radleybobins:在你的$ .post調用中。 – 2012-04-19 17:11:18

回答

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"); 
     } 
); 
+0

謝謝你只是寫同樣的:) – 2012-04-19 17:15:44

+0

@TobiasKrogh:偉大的思想家都認爲:-) – 2012-04-19 17:16:39

1

我認爲錯誤的是這一部分的內部:

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

在這裏,我想你想,當你正在使用最接近得到「TR」元素this是與上面相同。但在這裏this是$ .post imho的上下文。

你要麼需要綁定或者說在事件處理函數的頂部做var boxChange = $(this),和使用緩存的參考事後

+0

這是一個很好的想法,但我徹底解決了問題,問題依然存在。 – radleybobins 2012-04-19 17:09:30