-3

I have a html table :

<table>
 <tr >
  <td>name 1</td>
  <td>-</td>
  <td>-</td>
  <td>-</td>
</tr>
 <tr >
  <td>name 2</td>
  <td>-</td>
  <td>158,5</td>
  <td>-</td>
</tr>
</table>

I want to remove/hide lines where all values are eq to "-", what's the best way to do it ?

4

5 回答 5

2

This is easier if you add an id to your table, to avoid getting rows from other tables...

<table id="Example">
    ...

So here is an example...

$('#Example tr').each( function () {
    var self = $(this);
    var allEmpty = true;
    $('td', self).each( function () {
        if ($(this).text() !== '-') {
            allEmpty = false;
        }
    });

    if (allEmpty) {
        self.hide();
    }
});

This deletes a row where ALL fields are exactly -. You can modify this with more custom logic if you need it.

于 2012-12-24T14:37:24.873 回答
1

Use

$("button").click(function () {
    $("td").remove(":contains('-')");
});​

To delete the td with "-"

Link

于 2012-12-24T14:41:33.090 回答
1
$('tr').each(function() {
    var self = $(this);
    var EmptyCheck = true;
    $('td:not(:eq(0))', self).each(function() {
        if ($(this).text() !== '-') {
            EmptyCheck = false;
            console.log($(this));
        }
    });

    if (EmptyCheck) {
        self.hide();
        EmptyCheck = true;
    }
});​

Fiddle - http://jsfiddle.net/atif089/LWPbj/

于 2012-12-24T14:42:14.100 回答
0

You can use filter method.

$('table td').filter(function(){
   var txt = this.textContent || this.innerText;
   return txt === '-';
}).hide();

http://jsfiddle.net/4snwH/

于 2012-12-24T14:37:55.323 回答
0

Try this one.

$("table tr td").each(function(){

if($(this).html() == "-")
{
   $(this).css('display','none'); // if you want to hide
   $(this).remove(); // if you want to remove
}

});
于 2012-12-24T14:42:47.127 回答