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

javascript - Loop through a certain column in a table skipping the first column - Stack Overflow

programmeradmin5浏览0评论

I am wondering how I can loop through a certain column in a table using javascript or jquery.

Also is it possible to skip the first element in the column too.

I have a table of values and I am dynamically adding rows to the table.

So I have a table like

-------------
| A | d | G |
-------------
| D | e | H |
-------------
| c | f | I |
-------------

I have 3 text boxes and a button. Each text box will contain the data that will go into a cell in a new row. When the button is clicked, before adding rows, I want to check if the information in the text box is already added to the column that the text would normally go into.

So for example, if the textboxes contained (O,K,I) respectfully, I would want the code to check the first column for the presence of O, the second column for the presence of K and the third column to check for the presence of I. The code would find a match for I and then alert something.

Each table cell has a bit of text in it.

I am wondering how I can loop through a certain column in a table using javascript or jquery.

Also is it possible to skip the first element in the column too.

I have a table of values and I am dynamically adding rows to the table.

So I have a table like

-------------
| A | d | G |
-------------
| D | e | H |
-------------
| c | f | I |
-------------

I have 3 text boxes and a button. Each text box will contain the data that will go into a cell in a new row. When the button is clicked, before adding rows, I want to check if the information in the text box is already added to the column that the text would normally go into.

So for example, if the textboxes contained (O,K,I) respectfully, I would want the code to check the first column for the presence of O, the second column for the presence of K and the third column to check for the presence of I. The code would find a match for I and then alert something.

Each table cell has a bit of text in it.

Share Improve this question edited Nov 6, 2012 at 14:36 Wesley Skeen asked Nov 6, 2012 at 14:26 Wesley SkeenWesley Skeen 8,29513 gold badges44 silver badges57 bronze badges 3
  • Yes, it's possible. Can you be more specific about what you want to do? – David Thomas Commented Nov 6, 2012 at 14:28
  • Would the first element in each column be the header for the column? – Ian Commented Nov 6, 2012 at 14:31
  • @DavidThomas I have updated, Ian yes the top row would be the header – Wesley Skeen Commented Nov 6, 2012 at 14:37
Add a ment  | 

3 Answers 3

Reset to default 5

Update: Use :nth-child(columnIndex) in the selector. Note that in this case columnIndex is not zero based. i.e. if you want the 2nd column, use 2, not 1.

$('#foo td:nth-child(2)').each(function() {
    $(this).css('background-color', '#bbb');
});

Demo: http://jsfiddle/pMWAw/

Note: As David Thomas pointed out, using eq() will filter out just the first matched TD.

function looper(){
  var col = 5; // the column you want to look at 
  var rows = $('#yourtable tr');

  for(var i=1;i<rows.length;i++){
    var row = rows[i];
    var column = row.children('td')[col];
    // do whatever
  }
}
$('#tableId #td1').children().each(function(index)
{
  if ( index != 0 )
  {
    console.log($(this).html());
  }
});​
发布评论

评论列表(0)

  1. 暂无评论