简体   繁体   English

如何选择第一个和第二个td包含特定值的tr?

[英]How to select tr whose first and second td contains specific value?

I have table that looks like this: 我有这样的表:

在此处输入图片说明

Code as follows: 代码如下:

<table id="table1">
    <tr>
        <td>HU</td>
        <td>BIP</td>
        <td>Pannon</td>
    </tr>
    <tr>
        <td>HU</td>
        <td>BIP</td>
        <td>T-Mobile</td>
    </tr>
    <tr>
        <td>HU</td>
        <td>BIP</td>
        <td>Vodafone</td>
    </tr>
    <tr>
        <td>HU</td>
        <td>FUN</td>
        <td>Pannon</td>
    </tr>
    <tr>
        <td>HU</td>
        <td>FUN</td>
        <td>T-Mobile</td>
    </tr>
    <tr>
        <td>HU</td>
        <td>FUN</td>
        <td>Vodafone</td>
    </tr>
</table>

How to select last <tr> whose first <td> contains "HU" and second <td> contains "BIP". 如何选择最后一个<tr>其第一个<td>包含“ HU”,第二个<td>包含“ BIP”。

I tried to select it like this: 我试图这样选择它:

$('#table1').find('tr td:first-child:contains("HU") td:nth-child(2):contains("BIP")').last();

Shows nothing 什么也没显示

And like this: 像这样:

$('#table1').find('tr td:first-child:contains("HU")').parent().each(function(i,e){
    console.log($(e).children('td:contains("BIP")').parent().last());
});

Doesn't show last tr but all of them(that contains "HU" and "BIP") instead. 不显示最后一个tr而是全部(包含“ HU”和“ BIP”)。

Any idea? 任何想法?

You can do this using eq() and includes() methods. 您可以使用eq()includes()方法执行此操作。

eq() reduces the set of matched elements to the one at the specified index. eq()将匹配元素的集合减少到指定索引处的元素。

 var copy; $('table tr').each(function(i,e){ var first=$(this).find('td').eq(0).html().includes("HU"); var second=$(this).find('td').eq(1).html().includes("PIB"); if(first && second){ copy=$(e); } }); console.log($(copy).html()); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td>HU</td> <td>PIB</td> <td>Pannon</td> </tr> <tr> <td>HU</td> <td>PIB</td> <td>T-Mobile</td> </tr> <tr> <td>HU</td> <td>PIB</td> <td>Vodafone</td> </tr> <tr> <td>HU</td> <td>FUN</td> <td>Pannon</td> </tr> <tr> <td>HU</td> <td>FUN</td> <td>T-Mobile</td> </tr> <tr> <td>HU</td> <td>FUN</td> <td>Vodafone</td> </tr> </table> 

You can do something like this: 您可以执行以下操作:

 $(document).ready(function() { var target = $('table tr td:first-child:contains("HU")').parent().find('td:nth-child(2):contains("BIP")').last().parent(); console.log(target.html()); }); 
 <script src="https:code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script> <table> <tr> <td>HU</td> <td>BIP</td> <td>Pannon</td> </tr> <tr> <td>HU</td> <td>BIP</td> <td>T-Mobile</td> </tr> <tr> <td>HU</td> <td>BIP</td> <td>Vodafone</td> </tr> <tr> <td>HU</td> <td>FUN</td> <td>Pannon</td> </tr> <tr> <td>HU</td> <td>FUN</td> <td>T-Mobile</td> </tr> <tr> <td>HU</td> <td>FUN</td> <td>Vodafone</td> </tr> </table> 

It first selects all first td containing "HU" then selects their parent tr and then again filters the result by finding the second td containing "BIP" and then finally selects the last matched element. 它首先选择所有包含“ HU”的第一个td ,然后选择其父tr ,然后通过查找包含“ BIP”的第二个td再次过滤结果,然后最后选择最后一个匹配的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM