最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Read the first column values of a HTML table with jquery - Stack Overflow

programmeradmin0浏览0评论

I got a table:

<table id="ItemsTable" >​
    <tbody>
   <tr>
     <th>
       Number
     </th>
     <th>
       Number2
     </th>
    </tr>
  <tr>
    <td>32174711</td>     <td>32174714</td>
  </tr>
  <tr>
    <td>32174712</td>     <td>32174713</td>
  </tr>
</tbody>
</table>

I need the values 32174711 and 32174712 and every other value of the column number into an array or list, i'm using jquery 1.8.2.

I got a table:

<table id="ItemsTable" >​
    <tbody>
   <tr>
     <th>
       Number
     </th>
     <th>
       Number2
     </th>
    </tr>
  <tr>
    <td>32174711</td>     <td>32174714</td>
  </tr>
  <tr>
    <td>32174712</td>     <td>32174713</td>
  </tr>
</tbody>
</table>

I need the values 32174711 and 32174712 and every other value of the column number into an array or list, i'm using jquery 1.8.2.

Share Improve this question asked Oct 5, 2012 at 19:28 MiloMilo 3181 gold badge6 silver badges15 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 16
var arr = [];
$("#ItemsTable tr").each(function(){
    arr.push($(this).find("td:first").text()); //put elements into array
});

See this link for demo:

http://jsfiddle.net/CbCNQ/

You can use map method:

var arr = $('#ItemsTable tr').find('td:first').map(function(){
     return $(this).text()
}).get()

http://jsfiddle.net/QsaU2/

From jQuery map() documentation:

Description: Pass each element in the current matched set through a function, producing a new jQuery object containing the return values. . As the return value is a jQuery-wrapped array, it's very common to get() the returned object to work with a basic array.

// iterate over each row
$("#ItemsTable tbody tr").each(function(i) {
    // find the first td in the row
    var value = $(this).find("td:first").text();
    // display the value in console
    console.log(value);
});

http://jsfiddle.net/8aKuc/

well from what you have, you can use first-child

var td_content = $('#ItemsTable tr td:first-child').text()
// loop the value into an array or list

http://jsfiddle.net/Shmiddty/zAChf/

var items = $.map($("#ItemsTable td:first-child"), function(ele){
    return $(ele).text();
});

console.log(items);​
发布评论

评论列表(0)

  1. 暂无评论