2017-09-25 108 views
-2

我的代碼正常工作。使用Ajax訪問數據。從這個結果我想要顯示標籤中的特定值。當我正確地使用下面的代碼中值顯示從Ajax中獲取特殊值結果

var i = parseInt(doc.d.length); 
$("#<%=lblPresnet.ClientID %>").text(doc.d[5].PresentDays); 

,但我需要改變我的代碼

var i = parseInt(doc.d.length); 
$("#<%=lblPresnet.ClientID %>").text(doc.d[i].PresentDays); 

在這裏,我發現了一個錯誤

Uncaught TypeError: Cannot read property 'PresentDays' of undefined 

我不有一個想法,爲什麼它如此
我的阿賈克斯代碼

success: function (doc) { 
console.log(doc); 
var my_events = []; 
if (!doc.result) { 
$.each(doc.d, function (index, elem) { 
    my_events.push({ 
     id: elem.EventName, 
     title: elem.EventName, 
     start: elem.StartDate, 
     image: elem.ImageType, 
    }); 
    var i = parseInt(doc.d.length); 
    $("#<%=lblPresnet.ClientID %>").text(doc.d[5].PresentDays); 
}); callback(my_events); 
} 
} 

控制檯結果

{d: Array(6)}d: Array(6)0: {EventID: "00000000-0000-0000-0000-000000000000", EventName: "Present", StartDate: "2017-08-01", PresentDays: 1, AbsentDays: 0, …}1: {EventID: "00000000-0000-0000-0000-000000000000", EventName: "Present", StartDate: "2017-08-02", PresentDays: 2, AbsentDays: 0, …}2: {EventID: "00000000-0000-0000-0000-000000000000", EventName: "Present", StartDate: "2017-08-03", PresentDays: 3, AbsentDays: 0, …}3: {EventID: "00000000-0000-0000-0000-000000000000", EventName: "Present", StartDate: "2017-08-04", PresentDays: 4, AbsentDays: 0, …}4: {EventID: "00000000-0000-0000-0000-000000000000", EventName: "Present", StartDate: "2017-08-07", PresentDays: 5, AbsentDays: 0, …}5: {EventID: "00000000-0000-0000-0000-000000000000", EventName: "Present", StartDate: "2017-08-08", PresentDays: 6, AbsentDays: 0, …}length: 6__proto__: Array(0)__proto__: Object 

是他們這

回答

0

doc.d的長度的任何解決方案是6,但其指數從0到5去...我猜你應該改變那些行

var i = parseInt(doc.d.length - 1); 
$("#<%=lblPresnet.ClientID %>").text(doc.d[i].PresentDays); 
+0

@D le。謝謝,我忘了索引計數。 – Jks

+0

高興地幫助:) –