javascript - JQUERY - Getting data in a table cell which has a specific class - Stack Overflow

I have a table such as this below: <table id="Grid"><thead><tr><th>Mo

I have a table such as this below:

<table id="Grid">
 <thead>
  <tr>
     <th>Month</th>
     <th>Savings</th>
  </tr>
 </thead>
 <tbody>
  <tr>
     <td>January</td>
     <td class="k-dirty-cell">$100</td>
  </tr>
  <tr>
     <td>February</td>
     <td class="k-dirty-cell">$80</td>
  </tr>
  <tr>
     <td>March</td>
     <td>$98</td>
  </tr>
 </tbody>
</table>

I would like to iterate (or parse) through cells with class="k-dirty-cell" and get its value and validate whether it is null or not.

The class is always in the second column, so I am only interested in iterating through the rows of the second column - i.e., when cellIndex = 2.

I have tried this so far but I'm a bit stuck:

$("#Grid tbody").find('td').each(
    function () {
        debugger;

        // run for specific columns - where validation is needed
      //  var isDirty = cellToValidate.hasClass('k-dirty-cell');

       var isDirty = $(this).hasClass('k-dirty-cell');
        if (isDirty == true) {

            var cellContent = $(this).context.innerText;
            var cellIndex = $(this).context.cellIndex;

       //     alert(cellContent + cellIndex);

        }
    });

Could you please help if you can.

Many thanks.

I have a table such as this below:

<table id="Grid">
 <thead>
  <tr>
     <th>Month</th>
     <th>Savings</th>
  </tr>
 </thead>
 <tbody>
  <tr>
     <td>January</td>
     <td class="k-dirty-cell">$100</td>
  </tr>
  <tr>
     <td>February</td>
     <td class="k-dirty-cell">$80</td>
  </tr>
  <tr>
     <td>March</td>
     <td>$98</td>
  </tr>
 </tbody>
</table>

I would like to iterate (or parse) through cells with class="k-dirty-cell" and get its value and validate whether it is null or not.

The class is always in the second column, so I am only interested in iterating through the rows of the second column - i.e., when cellIndex = 2.

I have tried this so far but I'm a bit stuck:

$("#Grid tbody").find('td').each(
    function () {
        debugger;

        // run for specific columns - where validation is needed
      //  var isDirty = cellToValidate.hasClass('k-dirty-cell');

       var isDirty = $(this).hasClass('k-dirty-cell');
        if (isDirty == true) {

            var cellContent = $(this).context.innerText;
            var cellIndex = $(this).context.cellIndex;

       //     alert(cellContent + cellIndex);

        }
    });

Could you please help if you can.

Many thanks.

Share Improve this question edited Feb 27, 2014 at 1:20 t_plusplus asked Feb 27, 2014 at 1:09 t_plusplust_plusplus 4,2195 gold badges47 silver badges61 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

if your primary goal is I would like to iterate (or parse) through cells with class="k-dirty-cell" and get its value and validate whether it is null or not., the following will suffice:

$(function() {
  $('td.k-dirty-cell').each(function() {
    var cellValue = $(this).text();
    //do something with cellValue 
    if(cellValue) {
      //not null  
    } else {
      //null
    }
  });
});

EDIT: since there may be a case where the class is on the first column (per ment discussion), the following will select only second column items with the k-dirty-cell class:

$(function() {
    $('tr td:nth-child(2).k-dirty-cell').each(function() {
    var cellValue = $(this).text();
      alert(cellValue)
    //do something with cellValue 
    if(cellValue) {
      //not null  
    } else {
      //null
    }
  });
});

example fiddle: http://jsfiddle/tztLD/

$(".k-dirty-cell").each(function() {
    // your code
});

Should do the trick.

You only need to get all the td tags whose class is k-dirty-cell, so this would be enough:

$("#Grid td.k-dirty-cell").each(function() {
    if($(this).text().length > 0) {
       alert("value: " + $(this).text());
     }
} 

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信