<table border="1">
<thead>
<tr class="tr-background">
<td class="border-left">题目1</td>
<td>题目2</td>
<td>题目3</td>
<td>题目4</td>
<td>题目5</td>
<td>题目6</td>
<td>题目7</td>
</tr>
</thead>
<tr>
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
</tr>
</table>
获取按钮
<button type="button" class="layui-btn" onclick="tableJson(this)">获取数据</button>
js代码
function tableJson(obj) {
//找到要获取数据的表格
obj = $(obj).parent().siblings().children();
//定义一个空数组
var result = [];
obj.each(function () {
var tr = obj.find('tr');
for (var i = 1; i < tr.length; i++) { // 遍历表格中每一行的内容
var tds = $(tr[i]).find("td");
if (tds.length > 0) {
result.push({
"child_1": $(tds[2]).text(),
"child_2": $(tds[3]).text(),
"child_3": $(tds[4]).text(),
"child_4": $(tds[5]).text(),
"child_5": $(tds[6]).text(),
"child_6": $(tds[7]).text(),
"child_7": $(tds[8]).text()
})
}
}
var jsonData = [];
jsonData.push(result);
//将数组内容转化成json对象
JSON.stringify(jsonData);
console.log(jsonData)
});
}