0
我試圖在數據表中插入數據,但JavaScript CONSOLO說這張表是未定義的。這裏是我的代碼:如何將數據插入到jQuery數據表?
// ----creating the table----
miArray = [[11,22,33,44,55,"22/11/2012",99,86,99,1010,1111],[11,22,33,44,55,"11/06/2011",77,88,99,1010,1111],[11,22,33,44,55,"11/06/2011",77,88,99,1010,1111],[11,22,33,44,55,"11/06/2011",77,88,99,1010,1111],[11,22,33,44,55,"11/06/2011",77,88,99,1010,1111],[11,22,33,44,55,"11/06/2011",77,88,99,1010,1111],[11,22,33,44,55,"11/06/2011",77,88,99,1010,1111],[11,22,33,44,55,"11/06/2011",77,88,99,1010,1111],[11,22,33,44,55,"11/06/2011",77,88,99,1010,1111]]
var tabla= "<table name='tabla1' id='dataTable'><thead><tr><th>Tipo de documento</th><th>Tipo de documento</th><th>Tipo de documento</th><th>Codigo de documento</th><th>Descripcion de documento</th><th>Fecha de documento</th><th>Codigo de proveedor</th><th>Descripcion de proveedor</th><th>Importe</th><th>Importe IVA</th><th>Total a pagar</th></tr></thead><tbody>"
for (i=0;i<miArray.length;i++){
tabla = tabla + '<tr>'
for (j=0;j<miArray[i].length;j++){
tabla = tabla +'<td></td>'
}
tabla = tabla + '</tr>'
}
tabla = tabla +'</tbody></table>'
document.getElementById("tabla1").innerHTML = tabla
// -----inserting data--------
h=1
for (i=0;i<miArray.length;i++){
for (j=0;j<miArray[i].length;j++){
document.getElementById("tabla1").rows[h].cells[j] = miArray[i][j]
}
h++
}
OK,有DOM中的DIV寬度'tabla1'ID。 IT不是最好的名字... – Mikelon85 2012-04-04 11:45:34
完成了。我把div的id代替了桌子的id。感謝您的回答 – Mikelon85 2012-04-04 11:58:52
首先,確保腳本在文檔完全加載時執行,因此使用onload觸發器或將腳本放在頁面底部就足夠了。其次,在div上使用屬性.rows和.cells將不起作用。 div不是表格,因此這些屬性是無效的。 – trickyzter 2012-04-04 12:01:12