简体   繁体   中英

jQuery read all TD's table data

With jquery I am reading table:

$('#lc_searchresult > table > tbody > tr').each(function() {
            var data = $(this).find("td:eq(5)").html();
            alert(data);        
        });

it is working fine if TR tag has one TD inside like:

<tr>
<td>654321</td>       
</tr>

but If I am having two TD's then I am geting just first one:

<tr>
<td>654321</td>            
<td>13456</td>        
</tr>

How can I get all of TD's from TR with $(this).find("td:eq(5)").html()

$('#lc_searchresult > table > tbody > tr').each(function() {
  $(this).children('td').each(function(){
    var data = $(this).html();
  alert(data);
  })

});

Why eq it? You're using .each() which means you will return an array of <tr> 's.

 $('#lc_searchresult > table > tbody > tr').each(function() { var data = $(this).find('td').text(); alert(data); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="lc_searchresult"> <table> <tbody> <tr> <td>654321</td> <td>13456</td> </tr> <tr> <td>4353535</td> <td>3453553</td> </tr> </tbody> </table> </div> 

JSFiddle.

this should work,

**note thier is no need to use :eq selector here,

READ ABOUT JQUERY :eq selector

 $('#lc_searchresult > table > tbody > tr').each(function() { var data = $(this).html(); alert(data); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div id='lc_searchresult'> <table> <tr> <td>654321</td> <td>13456</td> </tr> </table> </div> 

td:eq(5) : mean you get data of TD with exactly index. so you can't get all data of TR with eq().

well when you write

var data = $(this).find("td");

the variable data contains an array of all the td's not just the first one

if you say

data.each(function(){
    alert($(this).html())
})  

you will get all the td's

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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