我正在尝试动态解析 JSON 数据并将数据填充到 html 表中的行中。
我写了以下测试,并没有真正看到它不应该工作的任何原因。
编辑:更正了两个语法错误。
<tohead>
<script>
var zoho = "?({"Products":[{"model":"UN55F8000BFXZA","phone":"1234567890","price":"2999.99","manufacturer":"Samsung","purchaseDate":"2013-07-26"}]});"
$(document).ready(function(){
processRecord(zoho);
});
function addRow(inc) {
VAR table = document.getElementById("dataTable");
VAR rowCount = table.rows.length;
VAR row = table.insertRow(rowCount);
VAR cell1 = row.insertCell(0);
VAR element1 = document.createElement("div");
element1.id = 'manufacturer' + inc;
element1.class = "text";
cell1.appendChild(element1);
VAR cell2 = row.insertCell(1);
VAR element2 = document.createElement("div");
element2.id = 'purchaseDate' + inc;
element2.class = "text";
element2.align = "center";
cell2.appendChild(element2);
VAR cell3 = row.insertCell(2);
VAR element3 = document.createElement("div");
element3.id = 'endingDate' + inc;
element3.class = "text";
element3.align = "center";
cell3.appendChild(element3);
};
function processRecord(zoho_data){
for (var i=0; i<zoho_data.length; i++){
addRow(i);
var phonenumber = zoho_data.Products[i].phone;
var zmanufacturer = zoho_data.Products[i].manufacturer;
var zmodel = zoho_data.Products[i].model;
var zpurchaseDate = zoho_data.Products[i].purchaseDate;
document.getElementById('manufacturer' + i).appendChild(zmanufacturer);
document.getElementById('purchaseDate' + i).appendChild(zpurchaseDate);
document.getElementById('endingDate' + i).appendChild(d1);
}
};
</script>
</tohead>
<HTML>
<HEAD>
</HEAD>
<BODY>
<TABLE id="dataTable" width="546">
</TABLE>
</BODY>
</HTML>
编辑:作为对评论的回应,我降低了上面代码的复杂性以缩小问题范围。下面的代码是只有 createElement 循环的新代码。
<HTML>
<HEAD>
<script>
$(document).ready(function(){
addRow();
});
function addRow() {
for (var i=0; i<4; i++)
{
VAR table = document.getElementById("dataTable");
VAR rowCount = table.rows.length;
VAR row = table.insertRow(rowCount);
VAR cell1 = row.insertCell(0);
VAR element1 = document.createElement("div");
element1.setAttribute('id', 'manufacturer' + i);
element1.setAttribute(class, 'text');
cell1.appendChild(element1);
VAR cell2 = row.insertCell(1);
VAR element2 = document.createElement("div");
element2.setAttribute('id', 'purchaseDate' + i);
element2.setAttribute(class, 'text');
element2.setAttribute(align, 'center');
cell2.appendChild(element2);
VAR cell3 = row.insertCell(2);
VAR element3 = document.createElement("div");
element3.setAttribute('id', 'endingDate' + i);
element3.setAttribute(class, 'text');
element3.setAttribute(align, 'center');
cell3.appendChild(element3);
}
};
</script>
</HEAD>
<BODY>
<TABLE id="dataTable" width="546" border="1">
<tr>
<td>boo</td>
</tr>
</TABLE>
</BODY>
</HTML>