我需要把表格行转换成JSON.
有任何想法吗?我有这个代码在这里,但它不工作.
function tableToJSON(tableID) { return $(tableID + " tr").map(function (row) { return row.descendants().pluck("innerHTML"); }).toJSON(); }
解决方法
function tableToJson(table) { var data = []; // first row needs to be headers var headers = []; for (var i=0; i<table.rows[0].cells.length; i++) { headers[i] = table.rows[0].cells[i].innerHTML.toLowerCase().replace(/ /gi,''); } // go through cells for (var i=1; i<table.rows.length; i++) { var tableRow = table.rows[i]; var rowData = {}; for (var j=0; j<tableRow.cells.length; j++) { rowData[ headers[j] ] = tableRow.cells[j].innerHTML; } data.push(rowData); } return data; }