我一直在浏览器中收到此错误(未捕获TypeError:无法读取未定义的属性'coverrc')
我的脚本如下:
var jsondata = '{"089d89f69b11a9b0a2c8dc156c356f48":{"rowid":"089d89f69b11a9b0a2c8dc156c356f48","id":"978-967-366-281-4","qty":"4","price":"14.40","options":{"coversrc":"\/images\/sized\/images\/uploads\/books\/Apa_Salah_Syiah_Final_CO-80x123.jpg"},"name":"Apa Salah Syiah","subtotal":57.6},"8b0a29a20d0bf409143f07735be32db2":{"rowid":"8b0a29a20d0bf409143f07735be32db2","id":"978-967-408-237-6","qty":"2","price":"23.40","options":{"coversrc":"\/images\/sized\/images\/uploads\/books\/perang-salib2_4-80x124.jpg"},"name":"Perang Salib Kedua","subtotal":46.8},"6ff46686900b2f7c0be05fdc468e78c2":{"rowid":"6ff46686900b2f7c0be05fdc468e78c2","id":"978-967-369-236-1","qty":"1","price":"24.00","options":{"coversrc":"\/images\/sized\/images\/uploads\/books\/Unfair-80x121.jpg"},"name":"Unfair Advantage Edisi Bahasa Melayu","subtotal":24},"total":"128.40","checkout_link":"http:\/\/bookcafe.com.my?dispatch=checkout.remote_add.978-967-366-281-4@4|978-967-408-237-6@2|978-967-369-236-1@1"}';
var myData = JSON.parse(jsondata);
var $grouplist = $('#bookcart');
$.each(myData, function() {
$('<li>' + this.options.coversrc + '</li>').appendTo($grouplist); // browser error points to here
});
答案 0 :(得分:2)
对象的某些属性不是对象(checkout_link
,total
)。
你可以这样做:
$.each(myData, function() {
if (!this.options) return;
$('<li>' + this.options.coversrc + '</li>').appendTo($grouplist);
});
答案 1 :(得分:2)
有total
和checkout_link
等属性没有option
属性。
所以在你的循环中,检查属性是否存在
var $grouplist = $('#bookcart');
$.each(myData, function () {
if (this.options) {
$('<li>' + this.options.coversrc + '</li>').appendTo($grouplist); // browser error points to here
}
});
演示:Fiddle