简体   繁体   English

如何使用jQuery每个函数从html表中提取数据

[英]how to extract data from html table using jQuery each function

I'm need to extract data from a html table to create a JSON array. 我需要从html表中提取数据以创建JSON数组。

Here is the structure of HTML table, 这是HTML表格的结构,

    <table class="tableClass">
        <thead class="tableHeaderClass" >
           <tr>
              <th>header_column1</th>
              <th>header_column2</th>
              <th>header_column3</th>
           </tr>
        </thead>
        <tbody class="tableBodyClass">
           <tr>
              <td>row1_column1</td>
              <td>row1_column2</td>
              <td>row1_column3</td>
           </tr>
           <tr>
             <td>row2_column1</td>
             <td>row2_column2</td>
             <td>row2_column3</td>
           </tr>
         </tbody>
    </table>

In my JavaScript function, I'm doing this 在我的JavaScript函数中,我正在这样做

  function() {

    var json = {
        header_column1 : '',
        header_column2 : '',
        header_column3 : ''
    };

    var data = [];
    $('tableClass').find('tbody').children('tr').each(function() {
         var $tds = $(this).find('td');
         json.header_column1 = $tds.eq(0).text();
         json.header_column2 = $tds.eq(1).text();
         json.header_column3 = $tds.eq(2).text();

         data.push(json);
    });

    return data;

 }

when I print my array, but I'm getting only 'row2_column1, row2_column2, row2_column3'. 当我打印数组时,我只得到“ row2_column1,row2_column2,row2_column3”。

Couldn't workout what I'm doing wrong/missing. 无法锻炼我在做错/错过的事情。 Would be great if you could help me out with this. 如果您能帮我解决这个问题,那就太好了。

$('tableClass')

Should be 应该

$('.tableClass')

When debugging jQuery, always make your selectors the initial suspects. 在调试jQuery时,请始终使选择器成为最初的可疑对象。 Check they're finding elements before continuing the chain. 在继续进行连锁之前,请检查他们是否正在寻找元素。 Thus: 从而:

alert($('tableClass').length)

...would give you 0 . ...会给你0

(Sidenote: (边注:

$('tableClass').find('tbody').children('tr')

can be shortened to 可以缩短为

$('tableClass').find('> tbody > tr')

You are using the wrong selector, $('tableClass') will try to select an element with a name of tableClass, ie: <tableClass></tableClass> You can use this instead: 您使用了错误的选择器, $('tableClass')将尝试选择一个名称为tableClass的元素,即: <tableClass></tableClass>您可以改用以下选择器:

$('.tableClass tbody tr')

Also since you are using an object each element in the array is going to reference the same object 另外,由于您使用的是对象,因此数组中的每个元素都将引用同一对象

//Clones the json object, might not be the most efficient method 
var newHeader = JSON.stringify(JSON.parse(json));
//Or just create a new object each iteration
var newHeader = {};
newHeader.header_column1 = $tds.eq(0).text();
newHeader.header_column2 = $tds.eq(1).text();
newHeader.header_column3 = $tds.eq(2).text();

data.push(newHeader);

See this question about object cloning. 请参阅有关对象克隆的问题

Beyond the selectors, the object syntax is invalid, it should be: 除选择器外,对象语法无效,应为:

var json = {
    header_column1: '',
    header_column2: '',
    header_column3:''
}

Also, you are updating and pushing the same object twice, so you will end up with two references to the last update on your array. 同样,您将两次更新并推送同一对象,因此最终将获得对数组上最后一次更新的两个引用。 In essence just getting 'row2_column1, row2_column2, row2_column3' twice on the array, instead of 'row1_column1, row1_column2, row1_column3' and 'row2_column1, row2_column2, row2_column3' 从本质上讲,只需在数组上两次获取“ row2_column1,row2_column2,row2_column3”,而不是“ row1_column1,row1_column2,row1_column3”和“ row2_column1,row2_column2,row2_column3”

Ditch the var json declaration on top and just do this within the 'each': 抛弃var json声明在最上面,然后在“ each”中执行此操作:

var json = {}
json.header_column1 = $tds.eq(0).text();
json.header_column2 = $tds.eq(1).text();
json.header_column3 = $tds.eq(2).text();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM