javascript - HTML 表格到数组 javascript

标签 javascript jquery html arrays

我之前发现了这个问题,这是答案,但我无法使其发挥作用。 所以问题是:我想使用 javascript 将表中的所有值获取到数组中

HTML 表格:

<table id="cartGrid">
  <thead>
       <tr>
          <th>Item Description</th>
          <th>Qty</th>
          <th>Unit Price</th>
          <th>Ext Price</th>
       </tr>
  </thead>
<tbody>
    <tr><td>Old Lamp</td><td>1</td><td>107.00</td><td>107.00</td>
    <tr><td>Blue POst</td><td>2</td><td>7.00</td><td>14.00</td>
</tbody>
</table>

JavaScript:

var myTableArray = [];
$("table#cartGrid tr").each(function() {
    var arrayOfThisRow = [];
    var tableData = $(this).find('td');
    if (tableData.length > 0) {
        tableData.each(function() { arrayOfThisRow.push($(this).text()); });
        myTableArray.push(arrayOfThisRow);
    }
});

alert(myTableArray);
<小时/>

我找到了另一种选择 - 但都返回一个空数组

var tableData = new Array();    
$('#cartGrid tr').each(function(row, tr){
    tableData[row]={
        "ItemDescription" : $(tr).find('td:eq(0)').text()
        , "Qty" :$(tr).find('td:eq(1)').text()
        , "UnitPrice" : $(tr).find('td:eq(2)').text()
        , "ExtPrice" : $(tr).find('td:eq(3)').text()
    }
}); 
tableData.shift();  // first row is the table header - so remove

最佳答案

根据您发布的无法正常工作的代码进行一些猜测,我建议使用 jQuery 进行以下操作:

// iterate over each of the <tr> elements within the
// <tbody>, using the map() method:
var details = $('tbody tr').map(function (i, row) {

    // creating an Object to return:
    return {

        // returning the key:value pair of
        // the hard-coded key-names against the
        // retrieved textContent (using the
        // HTMLTableRowElement's cells collection:
        'description': row.cells[0].textContent,
            'quantity': row.cells[1].textContent,
            'unitPrice': row.cells[2].textContent,
            'extPrice': row.cells[3].textContent
    }
// converting the map into an Array:
}).get();

console.log(details);

JS Fiddle demo .

或者,在纯 JavaScript 中:

// using Array.prototype.map(), with Function.prototype.call(), to treat
// the Array-like NodeList returned by document.querySelectorAll(), as an
// Array; iterating over the found <tr> elements within the <tbody>:
var details = Array.prototype.map.call(document.querySelectorAll('tbody tr'), function (row) {
    // the first argument of the anonymous function (here: 'row') is
    // the array-element of the array over which we're iterating.

    // here we return exactly the same as before:
    return {
        'description': row.cells[0].textContent,
            'quantity': row.cells[1].textContent,
            'unitPrice': row.cells[2].textContent,
            'extPrice': row.cells[3].textContent
    };
});

console.log(details);

引用文献:

关于javascript - HTML 表格到数组 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30467357/

相关文章:

javascript - 如何使用 Bootstrap 日期选择器格式化日期?

javascript - 这是 IE8 使用 Jquery append() 的错误吗

javascript - 通过javascript从只读文本区域获取更改值

javascript - 调用 OnChange 时运行 jquery 函数

javascript - 如何创建自动隐藏侧边栏

html - hta(HTML 应用程序)的替代方案是什么?

javascript - 尽管页面重新加载,如何限制 api 调用的速率?

javascript - 如何在不需要转义所有内容的情况下进行全局字符串替换?

javascript - jQuery 自动完成 : does not allow other text

asp.net - Jquery ajax 回发未命中服务器方法?