开发者

How to loop through a table and read the first colum in every rowspan

开发者 https://www.devze.com 2023-03-27 00:38 出处:网络
How do I read each and every value of the first column using jquery? I have the codewhich is not working.

How do I read each and every value of the first column using jquery?

I have the code which is not working.

$("foo:eq(1) tr").each(function(){
                alert(  $(this).find('td:first[rowspan]').text());
            });

 <table id="foo" border="1px"> 
    <tr>                      
      <td rowspan='3' id="date"> Monday5 </td> 
      <td id=开发者_StackOverflow"Name">Jim </td> 
    </tr> 
    <tr>
       <td id="Name"> Remy </td>    
</tr>    
</table> 


Fixed here: http://jsfiddle.net/tes9E/4/

First, the jQuery :eq selector is zero-based. Secondly, you need to put a # before selecting by ids, like so:

 $("#foo:eq(0) tr").each(function(){
   alert( $(this).find('td:first[rowspan]').text() );
 });

From jQuery documentation:

.eq( index )

index - An integer indicating the 0-based position of the element

Otherwise, everything looks great. Hope this helps!


$("#foo:eq(0) tr").each(function(){ //--> Noo # and eq starts from 0
                alert(  $(this).find('td:first[rowspan]').text());
            });

 <table id="foo" border="1px"> 
    <tr>                      
      <td rowspan='3' id="date"> Monday5 </td> 
      <td id="Name">Jim </td> 
    </tr> 
    <tr>
       <td id="Name"> Remy </td>    
</tr>    
</table>

Try this

EDIT

JS should be something like this

$("#foo:eq(0) td").each(function(){
    if($(this).attr('rowspan')!== undefined)
    {
        alert($(this).text())
    }
});

DEMo


// A simple function that returns an array of cells within a specific column
$.fn.column = function(i) {
  return $("tr td:nth-child(" + i + ")", this);
};

// Using that function we can now select all the cells in the first column
var $col1 = $table.column(1);

// And then iterate over each one and do something
$col1.each(function() {
  return alert($(this).text());
});
0

精彩评论

暂无评论...
验证码 换一张
取 消