简体   繁体   中英

How do I just get the cell values of the rows from a column (and not the header) of the table?

I just want to get the cell values from the rows of a column, but I do no want to get the undefined at the start. I think this undefined is the is the undefined td in the first tr of the table.

How do I just get the cell values of the rows from a column (and not the header) of the table?

my current fiddle gets me this in the the console:

undefined
123
456
789

or

undefined
abc
def
ghi

so the answer I would expect is

123
456
789

or

abc
def
ghi

Note Similar question here which has helped get to this point

You can exclude the first header row with #mytable tr:not(:first) selector:

$('#mytable tr:not(:first)').each(function() {
    var customerId = $(this).find("td").eq(0).html();
    console.log(customerId)
});

Simple check would solve it:

$('#mytable tr').each(function() {
  if ($(this).find("td").eq(0).length) {
    var customerId = $(this).find("td").eq(0).html();
    console.log(customerId)
  }
});

Fiddle: https://jsfiddle.net/praveenscience/cLv9gk08/

Here you go: Fiddle

$('#mytable tr').each(function() {
    var customerId = $(this).find("td:first").html();    
});


//OPTION 2 
//To select a particular cell, you can reference them with an index:
$('#mytable tr').each(function(index) {
if(index !== 0){
   var customerId = $(this).find("td").eq(0).html();    
    console.log(customerId);
}

});

It would be better to put the header row into a <thead> section and all the rows into a <tbody> so you can then use $('#mytable tbody tr').each(.....); which will automatically exclude the header.

var customerId = $(this).find(“ td”)。html();

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