我使用$ .ajax({})和外部php腳本創建了一個用於更新和處理數據的Javascript對象。調用對象時從對象返回的表元素包含一些隱藏的tr(使用「hidden」類,它被設置爲display:none)。JQuery處理程序無法訪問Ajax函數後的數據
這裏是在對象內部的顯示功能:
this.displayData = function(location_div) {
var location_div = location_div;
$.ajax({
url:"display_data.php",
type: "POST",
success:function(response){
$(location_div).html(response);
},
error:function(response){alert("Error Displaying Problems." + response)},
});
};
,其顯示:
<div id="location_div">
<table>
<tbody>
<tr class="hidden"></tr>
<tr class="hidden"></tr>
<tr class="hidden"></tr>
<tr class="hidden"></tr>
</tbody>
<table>
</div>
(注: 「#location_div」 是傳遞的元素爲 「displayData(location_div)」)
我遇到的問題是當JQuery試圖訪問返回的數據。我有一個函數可以查找所有的tr,將它們分別推入一個數組,然後逐個淡入每一個數組。
var elements_array = new Array();
$("#location_div table tbody").children().each(function() {
elements_array.push(this);
});
//function for fading in each element in the array
fadeAllIn(elements_array);
據我所知,是Ajax的加載元件不是DOM的一部分,因此人們需要使用JQuery的功能,諸如$(元件)。在();或$(element).live();然而,我沒有試過的東西已經奏效。該表正在加載,但JQuery似乎無法使用.each()選擇每個tr,因爲該表是Ajax加載的。
有沒有人知道這個解決方案/解決方法?有沒有比使用JQuery淡入表中的每個tr更好的解決方案?
你可以用你的代碼製作一個[pastebin](http://pastebin.com/)。我想看看什麼是什麼聲明什麼時候。 –
你在哪裏調用這個方法..看起來你是在數據被填充爲「Ajax is Asyn'之前調用方法 –
如果你要爲你的」數組推動器「創建一個函數,然後稱之爲成功從你的ajax函數回調? 編輯:剛纔看到@ face的回答基本上是這樣做的。 – Spencer