简体   繁体   English

jQuery:查找包含两个具有特定文本的单元格的表行

[英]jQuery : find table row containing two cells that have specific text

I have a table with two columns for codes.I named them Main Code and Sub Code . 我有一个表格,其中有两列Main Code ,我将它们命名为Main CodeSub Code I have 2 text box for search in table that I want when user fill up them and press Enter key, change background-color of the row that contain Main Code and Sub Code . 当用户填写表格并按Enter键并更改包含Main CodeSub Code的行的background-color时,我在想要的表中有2个用于搜索的文本框。 I see this topic : 我看到这个话题:

find table row containing table cell containing specific text 查找包含包含特定文本的表格单元格的表格行

but it don't know how to check 2 cell instead of one cell.please help me 但它不知道如何检查2个单元格而不是一个单元格。请帮助我

thanks 谢谢

Edit 1) 编辑1)

this is an example of my table: 这是我的桌子的一个例子:

<table border="1" cellpadding="0" cellspacing="0" style="width: 400px; text-align: center;">
    <tr style="background-color:Aqua">
        <th style="width: 25%">
            Main Code</th>
        <th style="width: 25%">
            Sub Code</th>
        <th style="width: 25%">
            ID</th>
        <th style="width: 25%">
            Desc</th>
    </tr>
    <tr>
        <td style="width: 25%">
            10000</td>
        <td style="width: 25%">
            1</td>
        <td style="width: 25%">
            454</td>
        <td style="width: 25%">
            some Desc</td>
    </tr>
    <tr>
        <td style="width: 25%">
            10000</td>
        <td style="width: 25%">
            2</td>
        <td style="width: 25%">
            123</td>
        <td style="width: 25%">
            some</td>
    </tr>
    <tr>
        <td style="width: 25%">
            10001</td>
        <td style="width: 25%">
            1</td>
        <td style="width: 25%">
            454</td>
        <td style="width: 25%">
            some</td>
    </tr>
</table>
    searchTerm1 = $('input.maincode').val();
    searchTerm2 = $('input.subcode').val();
    $("td:contains("+searchTerm1+")")
        .siblings()
        .filter(":contains("+searchTerm2+")").parent('tr').css('background','yellow');

http://jsfiddle.net/JrrDn/47/ http://jsfiddle.net/JrrDn/47/

The code might be a little messy but the concept holds. 代码可能有点混乱,但是这个概念成立了。 Basically you want to iternate through each tr and check if the nth-child(1) and nth-child(2) are the values you are looking for. 基本上,您要遍历每个tr并检查nth-child(1)和nth-child(2)是否为您要查找的值。

Use jquery for this: 为此使用jQuery:

 $('table th:contains("Main Code")').parent().css('background-color', 'red'); 
 $('table th:contains("Sub Code")').parent().css('background-color', 'red'); 

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

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