在你当前的代码,该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