js遍历table中的数据,并组装成json
1 function fore(){ 2 var temp = ""; 3 var tabLen = document.getElementById("tableID"); 4 var jsonT = "{pieTes:["; 5 for(var i = 0; i < tabLen.rows.length;i++){ 6 for(var j = 0;j<tabLen.rows[i].cells.length;j++){ 7 //temp +=tabLen.rows[i].cells[j].innerHTML; 8 if(i == 0 && j == 0){ 9 jsonT +="{name:\""+tabLen.rows[i].cells[0].innerHTML+"\",gw:\""+tabLen.rows[i].cells[1].innerHTML+"\"}" 10 }else{ 11 jsonT +=",{name:\""+tabLen.rows[i].cells[0].innerHTML+"\",gw:\""+tabLen.rows[i].cells[1].innerHTML+"\"}" 12 } 13 14 } 15 } 16 jsonT += "]}"; 17 console.log(jsonT); 18 } 19