删除表中的空td

时间:2019-04-12 07:20:34

标签: javascript jquery

我有一个表和jquery代码,我需要:如果td为空,则删除tr的{​​{1}}父级。

我该怎么做?

我的代码不起作用。在我的示例中,我有空的td。我要删除td父母。为什么我的代码不起作用?我究竟做错了什么?我为每个tr搜索td,如果td为空,则删除td父对象。但是不行。不是错误。

tr
$('#table_serv tr').each(function(){ 
    if( $(this).find('td').text().trim() === '' )
        $(this).find('td').parent().remove(); // if it is empty, then remove parent tr
});

3 个答案:

答案 0 :(得分:1)

问题是

$(this).find('td').text()

将为您提供整行的单个文本,而不是单个td的文本。检查它在做什么很容易:

console.log($(this).find("td").text())

对于第一行,它会像这样:

:0,00       0,00 €  0,00    0,00 €

您似乎要查找一个空单元格(而不是空行)-因此您需要迭代各个td

一种方法是:

$('#table_serv tr td').each(function() { 
    if ($(this).text().trim() === '')
        $(this).parent().remove(); 
});

答案 1 :(得分:0)

我猜在您的示例中,您想删除第一行,以便代码可以正常工作。 经过一些修改后,您可以将其缩短一点。

To Process

答案 2 :(得分:0)

代码几乎不错!您只需要同时循环td。

$('#table_serv tr').each(function(){ 
    var tds = $(this).find('td');
    tds.each(function(){
      if ($(this).text() === ''){
       $(this).parent().remove();
      }
    });    
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table border="0" class="document-as" color="black" id="table_serv" style="font-size: 12px;border-right:0" width="100%">
    <thead>
        <tr>
            <th style="text-align: center;color: white;font-weight: 600; width:5%"></th>
            <th style="text-align: left;color: white;font-weight: 600;width:35%;"><span style="font-family:verdana,geneva,sans-serif;"><span style="color:#000000;"><strong>Description</strong></span></span>
            </th>
            <th style="text-align: center;color: white;font-weight: 600;width:25%;"><span style="font-family:verdana,geneva,sans-serif;"><span style="color:#000000;"><strong>Selling Price</strong></span></span>
            </th>
            <th style="text-align: center;color: white;font-weight: 600;width:15%;"><span style="font-family:verdana,geneva,sans-serif;"><span style="color:#000000;"><strong>Quantity</strong></span></span>
            </th>
            <th style="text-align: center;color: white;font-weight: 600;width:15%"><span style="font-family:verdana,geneva,sans-serif;"><span style="color:#000000;"><strong>Total</strong></span></span>
            </th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td class="serv-nomer" style=""><span style="font-family:verdana,geneva,sans-serif;">:0,00</span>
                <br> &nbsp;</td>
            <td style="text-align:left;"><span class="en_us" style=""></span><span class="de_de" style=""></span></td>
            <td class="serv-price" style=""><span>0,00 €</span></td>
            <td style=""><span style="font-family:verdana,geneva,sans-serif;">0,00</span></td>
            <td style=""><span style="font-family:verdana,geneva,sans-serif;"><strong>0,00 €</strong></span></td>
        </tr>
        <tr>
            <td class="serv-nomer" style=""><span style="font-family:verdana,geneva,sans-serif;">9:324,00</span>
                <br> &nbsp;</td>
            <td style="text-align:left;"><span class="en_us" style="">Justification safety (GOST-Norm)</span><span class="de_de" style="">Begründung Sicherheit (GOST-Norm)</span></td>
            <td class="serv-price" style=""><span>324,00 €</span></td>
            <td style="">1,00</td>
            <td style=""><span style="font-family:verdana,geneva,sans-serif;"><strong>324,00 €</strong></span></td>
        </tr>
        <tr>
            <td class="serv-nomer" style=""><span style="font-family:verdana,geneva,sans-serif;">SER20:432,00</span>
                <br> &nbsp;</td>
            <td style="text-align:left;"><span class="en_us" style="">Certificate TR CU 004/020/2011 for 5 Years</span><span class="de_de" style="">Zertifikat TR CU 004/020/2011 für 5 Jahre</span></td>
            <td class="serv-price" style=""><span>432,00 €</span></td>
            <td style="">1,00</td>
            <td style=""><span style="font-family:verdana,geneva,sans-serif;"><strong>432,00 €</strong></span></td>
        </tr>
        <tr>
            <td class="serv-nomer" style=""><span style="font-family:verdana,geneva,sans-serif;">7:34,00</span>
                <br> &nbsp;</td>
            <td style="text-align:left;"><span class="en_us" style="">Declaration TR ZU 04/2011 for  5 Years</span><span class="de_de" style="">Deklaration TR ZU 04/2011 for 5 Jahre</span></td>
            <td class="serv-price" style=""><span>34,00 €</span></td>
            <td style="">1,00</td>
            <td style=""><span style="font-family:verdana,geneva,sans-serif;"><strong>34,00 €</strong></span></td>
        </tr>
        <tr>
            <td class="serv-nomer" style=""><span style="font-family:verdana,geneva,sans-serif;">:0,00</span>
                <br> &nbsp;</td>
            <td style="text-align:left;"><span class="en_us" style=""></span><span class="de_de" style=""></span></td>
            <td class="serv-price" style=""><span>0,00 €</span></td>
            <td style=""><span style="font-family:verdana,geneva,sans-serif;">0,00</span></td>
            <td style=""><span style="font-family:verdana,geneva,sans-serif;"><strong>0,00 €</strong></span></td>
        </tr>
        <tr>
            <td class="serv-nomer" style=""><span style="font-family:verdana,geneva,sans-serif;">9:23,00</span>
                <br> &nbsp;</td>
            <td style="text-align:left;"><span class="en_us" style="">Justification safety (GOST-Norm)</span><span class="de_de" style="">Begründung Sicherheit (GOST-Norm)</span></td>
            <td class="serv-price" style=""><span>23,00 €</span></td>
            <td style="">1,00</td>
            <td style=""><span style="font-family:verdana,geneva,sans-serif;"><strong>23,00 €</strong></span></td>
        </tr>
        <tr>
            <td class="serv-nomer" style=""><span style="font-family:verdana,geneva,sans-serif;">SER20:43,00</span>
                <br> &nbsp;</td>
            <td style="text-align:left;"><span class="en_us" style="">Certificate TR CU 004/020/2011 for 5 Years</span><span class="de_de" style="">Zertifikat TR CU 004/020/2011 für 5 Jahre</span></td>
            <td class="serv-price" style=""><span>43,00 €</span></td>
            <td style="">1,00</td>
            <td style=""><span style="font-family:verdana,geneva,sans-serif;"><strong>43,00 €</strong></span></td>
        </tr>
        <tr>
            <td colspan="4" style=""><span>Sub Total</span></td>
            <td><span>856,00 €</span></td>
        </tr>
        <tr>
            <td colspan="4" style=""><span style="font-family:verdana,geneva,sans-serif;">Discount</span></td>
            <td><span style="font-family:verdana,geneva,sans-serif;">0,00 €</span></td>
        </tr>
        <tr>
            <td colspan="4" style=""><span style="font-family:verdana,geneva,sans-serif;">Total</span></td>
            <td><span style="font-family:verdana,geneva,sans-serif;">856,00 €</span></td>
        </tr>
        <tr>
            <td colspan="4" style=""><strong>&nbsp;&nbsp;&nbsp;&nbsp; Grand Total</strong></td>
            <td style=""><span><span><strong>1343,92 €</strong></span></span>
            </td>
        </tr>
    </tbody>
</table>