开发者

run a code for all rows of table jQuery

I am using this code to hide the element in sixth column of a table on a condition (I mean if text of span fourth column of table is "0").

But this code just works for first row of table.

How can i do this function for all rows of target table ?

if ($('#table  tr  t开发者_如何学编程d:eq(4) > span').text() == "0") {
            $('#table  tr  td:eq(6) > .PrintReport').hide();
        }


If you could post the full HTML structure of tr then you'd get more optimized solutions. Looking at your existing code you could do something like this:

$('#table tr').each(function() {
  var text = $('td:eq(4) > span', this).text();

  $('td:eq(6) > .PrintReport', this).toggle(text != '0');
});

Notice that inside the loop I'm using this as the context in the selectors.

EDIT: Explaining some of the above code -

  //This runs the selector in the context of 'this' (the table row)
  //It is functionally equivalent to $(this).find('td:eq(6) > .PrintReport')
  $('td:eq(6) > .PrintReport', this)

  //This will .show() it if the expression evaluates to true
  //and hide if false
  .toggle(text != '0')


$('#table tr').each(function() {  
     var text = $('td:eq(4) > span', this).text();   
     if(text=='0')   
     $('td:eq(6)').find('+.PrintReport').hide();
    });


$('table tr').filter(function () {
    return $('td:eq(4) > span', this).text() == "0";
}).find('td:eq(6) > .PrintReport').hide();

UPDATE: If you're looking for the 4th and 6th column (in human measures) you have to use 3 and 5 respectively for :eq, because it works with 0-based indexes. I left my example as it was, just wanted to give this warning.

I also added a working demo: jsFiddle Demo


Gotta run but this should work: http://jsfiddle.net/thomas4g/aAQNC/4/ Loop through each row with .each...

 $("#table tr").each(function() {
      $(this).children("td:eq(6) > .PrintReport").hide();
      });


I think you should use each() for it to work for every row. Please try this(just wrote it on the fly and not tested):

$('#table  tr  td:eq(4) > span').each(function(){
  if ($(this).html() == "0") {
            $(this).closest("tr").find('.PrintReport').hide();
        }
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜