在你當前的代碼,該value
是一個對象,但是,警報只能顯示string
,所以它會使用.toString
轉換您value
爲一個字符串,然後成爲"[Object Object]"
。
要顯示value
作爲鍵值對使用JSON.stringify(value)
,使之成爲json
再次:
success: function(response){
console.log(response);
if(response.success){
$.each(response.vote, function(index, value){
alert(JSON.stringify(value));
});
}
}
,如果你只是想訪問該值的屬性,用自己的鑰匙應該工作:
success: function(response){
console.log(response);
if(response.success){
$.each(response.vote, function(index, value){
// This will alert each items' `bundle` value.
// It's enough in your case, but you may have to check if the target attribute you want to alert is also an object.
alert(value.bundle);
});
}
}
每一個'value'都是一個'object',alert會使它成爲一個字符串,這會在值上隱藏一個.toString,所以你得到'[Object Object]'。你可以嘗試'alert({})',這會給你相同的結果。如果你只是想看到這些鍵值對,你可以再次使用'json',alert(JSON.stringify(value))',但是如果你只想訪問它的值,可以使用'value.branch' ...等等。 – fuyushimoya
所以任何想法如何在警報提示中呈現它像一個字符串?就像我要去顯示分支一樣,我得到每個分支。 –
'alert'將對象轉換爲字符串,''GrandParent'Object'類中的'toString'方法使用原型鏈調用,因此它警告'[object Object]'。使用'console.log'。 – Tushar