其實我有一個全局JSON,當我試圖解析其循環中的值時,它顯示錯誤「未捕獲類型錯誤:無法讀取值'名稱'未定義「。我嘗試了很多,但我仍然無法找出任何解決方案。未捕獲類型錯誤:無法讀取未定義的值'名稱'
$(document).ready(function(){
var productJSON = [
{id:"1001",name:"Hopper1",image:"images/290161k.jpg"},
{id:"1002",name:"Hopper2",image:"images/290161k.jpg"},
{id:"1003",name:"Hopper3",image:"images/290161k.jpg"},
{id:"1004",name:"Hopper4",image:"images/290161k.jpg"},
{id:"1005",name:"Hopper5",image:"images/290161k.jpg"},
{id:"1006",name:"Hopper6",image:"images/290161k.jpg"},
{id:"1007",name:"Hopper7",image:"images/290161k.jpg"},
{id:"1008",name:"Hopper8",image:"images/290161k.jpg"}
];
var a=0;
for(var i=0;i<productJSON.length;i++){
var pagedisplay = '';
for(var j=0;j<2;j++){
var generatedProductDisplay = '';
generatedProductDisplay = '<div id="'+productJSON[a].id+'" class="productDiv"><a class="productLink" href="#"><center><div class="productImage"><img src="'+productJSON[a].image+'" width="100%" height="200px" alt="'+productJSON[a].name+'"></div><div><p class="productName">'+productJSON[a].name+'</p></div></center></a></div>';
pagedisplay = pagedisplay+generatedProductDisplay;
a++;
}
pagedisplay = pagedisplay+'<br/>';
$(".productDisplay").append(pagedisplay);
}
$(".productDiv").live("click",function(){
alert("Hello");
});
});
這是HTML代碼
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome to Nitin Agro Industries, Chhatarpur</title>
<link href="styles/main.css" type="text/css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"
type="text/javascript"></script>
<script src="productsDisplay.js" type="text/javascript"></script>
</head>
<body>
<center>
<div class="page-wrap">
<div class="centerContent">
<h1>Explore our Product Catalog</h1>
<div class="centerText">
<center>
<div class="hideShowDiv">
skdddddddddddd
</div>
<div class="productDisplay"></div>
</center>
</div>
</div>
</div>
</center>
</body>
</html>
我們怎麼能可能幫助你沒有看到JSON和代碼? – 2012-01-18 13:46:53
粘貼源代碼。 – 2012-01-18 13:47:16
陳述顯而易見:問題是你正在訪問一個不存在的屬性。解決方法是不訪問它或確保它存在。 – 2012-01-18 13:47:53