jQuery 将所有表 td 值放入数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5991011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:10:38  来源:igfitidea点击:

Getting all the table td values into array

jquery

提问by Lamps

I need to get all the td values into a string array. Following is my code

我需要将所有 td 值放入一个字符串数组中。以下是我的代码

var tr = "#tblPurchaseOrders tr.PO1";

     $(tr).each(function(index, tr) {
                    var lines = $(tr + " td").map(function(ind, td) {
                        var ret = {};
                        ret[ind] = $(td).text();
                        return ret;
                    }).get();

    // I need to some more thing here 

    // I need to store all the td values in to lines variable.. This is not working for me.
    // Am I missing some thing?

    });

Thanks.

谢谢。

回答by Darin Dimitrov

Try like this:

像这样尝试:

$('#tblPurchaseOrders tr.PO1').each(function(index, tr) {
    var lines = $('td', tr).map(function(index, td) {
        return $(td).text();
    });
    // Here lines will contain an array of all td values for the current row:
    // like ['value 1', 'value 2', 'value 3']

});

回答by Gabriel Ramirez

Darin Dimitrov's code was very helpful. I modified it to pull from the click.

Darin Dimitrov 的代码非常有帮助。我将其修改为从点击中拉出。

$('#tblPurchaseOrders tr').click(function(){
    $(this, 'tr').each(function(index, tr) {
       var lines = $('td', tr).map(function(index, td) {
        return $(td).text();
    });
//This assumes that you have a table with an id of tblPurchaseOrders and that you have two cells of data
        alert(lines[0] + ' ' + lines[1]);
    })
});

回答by David Cullen

Assuming rowis the result of a jQuery selector, the data can be retrieved as an array using the following:

假设row是 jQuery 选择器的结果,则可以使用以下命令将数据作为数组检索:

function get_row_as_array(row) {
    var data = $('td', row).map(function(index, value) {
        return $(value).text();
    });
    return data;
}

I submit this answer as the general case for those who only need the data from one table row.

对于那些只需要一个表格行中的数据的人,我将此答案作为一般情况提交。

$(document).ready(function() {
  function get_row_as_array(row) {
    var data = $('td', row).map(function(index, value) {
      return $(value).text();
    });
    return data;
  }

  var row = $('#alphabet tr:eq(1)');
  var result = get_row_as_array(row);
  $('#alphabet tr:eq(2) td:eq(2)').html(result[2]);
});
table {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  width: 100%;
}
td,
th {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}
tr:nth-child(even) {
  background-color: #dddddd;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>

<head>
  <title>Get Table Row as Array</title>
</head>

<body>
  <table id="alphabet">
    <tr>
      <th>0</th>
      <th>1</th>
      <th>2</th>
      <th>3</th>
      <th>4</th>
    </tr>
    <tr>
      <td>Alpha</td>
      <td>Bravo</td>
      <td>Charlie</td>
      <td>Delta</td>
      <td>Echo</td>
    </tr>
    <tr>
      <td>Empty</td>
      <td>Empty</td>
      <td>Empty</td>
      <td>Empty</td>
      <td>Empty</td>
    </tr>
  </table>
</body>

</html>