javascript - loop html table using jquery and for loop (not for each) - Stack Overflow

I have a HTML table<table id="mytab"><thead><th>col1<th><th>co

I have a HTML table

 <table id="mytab">
  <thead>
    <th>col1</th>
    <th>col2</th>
  </thead>
  <tbody>
  <tr>
    <td>1</td>
    <td>2</td>
  </tr>
  <tr>
     <td>11</td>
     <td>22</td>
  </tbody>
 </table>

and i am writing the jquery function

  function LoopTable()
  {
      $row = $('#mytab tbody >tr"); //here I have successfully find all the rows.

     //now i want to loop on rows and find each column row
     for (var i=0; i<$rows.length; i++)
     {
         //need something here to find col data

     }

what I should use to have column values from row

I have a HTML table

 <table id="mytab">
  <thead>
    <th>col1</th>
    <th>col2</th>
  </thead>
  <tbody>
  <tr>
    <td>1</td>
    <td>2</td>
  </tr>
  <tr>
     <td>11</td>
     <td>22</td>
  </tbody>
 </table>

and i am writing the jquery function

  function LoopTable()
  {
      $row = $('#mytab tbody >tr"); //here I have successfully find all the rows.

     //now i want to loop on rows and find each column row
     for (var i=0; i<$rows.length; i++)
     {
         //need something here to find col data

     }

what I should use to have column values from row

Share Improve this question asked Jun 30, 2013 at 10:13 Mandeep SinghMandeep Singh 2,1468 gold badges19 silver badges32 bronze badges 5
  • dump the $row value , you may get clue – Ichigo Kurosaki Commented Jun 30, 2013 at 10:18
  • Meaning: try console.dir($row) in your browsers dev console (press f12 in IE/Chrome to open it) – OzrenTkalcecKrznaric Commented Jun 30, 2013 at 10:21
  • And you don't want to use each() method? You need each td value, or? – sinisake Commented Jun 30, 2013 at 10:21
  • because I want to work with only some columns and rows thats why I don't want to use each() method, I want somthing $rows[i].cells[0].val() – Mandeep Singh Commented Jun 30, 2013 at 10:25
  • a lot of mistakes are in OP code. such as ' closing with " and $row is initialize and $rows is used in for loop – Manoj Yadav Commented Jun 30, 2013 at 10:27
Add a ment  | 

2 Answers 2

Reset to default 3

Updated code

$(document).ready(function() {
    var rows = $('#mytab tbody >tr');
    var columns;
    for (var i = 0; i < rows.length; i++) {
        columns = $(rows[i]).find('td');
        for (var j = 0; j < columns.length; j++) {
            console.log($(columns[j]).html());
        }
    }
});
for (var i=0; i<$rows.length; i++)
{
    $.each($row[i].children("td"), function(key, value) {
         console.log(value);
    });
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744860300a4597659.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信