获取多个表格数据 逐行逐项转化成json数据 表格主体


 

                    

                        

                            题目1

                            

                            

                            

                            

                            

                            

                        

                    

                 

                    

                    

                    

                    

                    

                    

                    

                    

                    

                

题目2 题目3 题目4 题目5 题目6 题目7
0 1 2 3 4 5 6 7

获取按钮

 获取数据

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)

        });

    }

你可能感兴趣的:(获取多个表格数据 逐行逐项转化成json数据 表格主体)