日期:2014-05-16  浏览次数:20345 次

TypeError: jsonObj[index] is undefined

$(function(){
//绘制表格
var jsonObj=${json};
var count=jsonObj.length;
var rowCount=Math.ceil(count/4);
var cellCount=4;
var table=$("<table cellpadding=\"0\" cellspaceing=\"0\" style=\"margin-left:20px;\" width=\"750\">");
 table.appendTo($("#createtable"));
 for(var i=0;i<rowCount;i++)
 {
    var tr=$("<tr></tr>");
tr.appendTo(table);
for(var j=0;j<cellCount;j++)
{
var index=i*cellCount+j;
   var td=$("<td><input type='checkbox' value='"+jsonObj[index].permissionValue+"' id='"+jsonObj[index].permissionTypeId
   +"'/><label>"+jsonObj[index].name+"</label></td>");
   td.appendTo(tr);
}
 }
 trend.appendTo(table);
 $("#createtable").append("</table>");
 
 //初始化权限值
 $("input[type='checkbox']").each(function(){
 var permission=$(this).attr("value");
 if(permission==1){
 console.log($(this).attr("checked"));
 $(this).attr("checked","checked");
 }
 });

});


json 字符串格式如下:

[{"id":null,"personId":10250,"permissionTypeId":5,"permissionValue":0},{"id":null,"personId":10250,"permissionTypeId":6,"permissionValue":1}]

json为后台传过来的json字符串,然后遍历这个字符串后将json字符串中信息通过table表现出来,但是每次都报TypeError: jsonObj[index] is undefined 这个错误,我在firebug里通过右击查看元素可以看到生成的<input>标签里能够正常取到值,但是这个错误导致在这代码之后的js没法正常运行,求助?

------解决方案--------------------
var index=i*cellCount+j;
如果这里计算出来定的索引值大于数组的最大索引值就会出现这个问题。
这个地方你需要重新写公式计算

------解决方案--------------------
用var jsonObj=${json};
var obj = eval (jsonObj);
这样 因为你的字符串已经有[]了