简体   繁体   English

jQuery - 查找包含包含特定文本的表格单元格的表格行

[英]jQuery - find table row containing table cell containing specific text

I need to get a tr element which contains a td element which contains specific text.我需要获取一个tr元素,其中包含一个td元素,该元素包含特定的文本。 The td will contain that text and only that text (so I need text = 'foo' not text contains 'foo' logic). td将包含该文本并且仅包含该文本(所以我需要text = 'foo'而不是text contains 'foo'逻辑)。

So I need the equivalent of the following 'pseudo jQuery':所以我需要相当于下面的“伪 jQuery”:

var tableRow = $(table td[text = 'foo']).parent('tr');

Can anyone provide the correct syntax?谁能提供正确的语法?

You can use filter() to do that:您可以使用filter()来做到这一点:

var tableRow = $("td").filter(function() {
    return $(this).text() == "foo";
}).closest("tr");

I know this is an old post but I thought I could share an alternative [not as robust, but simpler] approach to searching for a string in a table.我知道这是一篇旧帖子,但我想我可以分享一种替代方法 [不是那么强大,而是更简单] 来搜索表中的字符串。

$("tr:contains(needle)"); //where needle is the text you are searching for. //其中 needle 是您要搜索的文本。

For example, if you are searching for the text 'box', that would be:例如,如果您正在搜索文本“框”,则为:

$("tr:contains('box')");

This would return all the elements with this text.这将返回带有此文本的所有元素。 Additional criteria could be used to narrow it down if it returns multiple elements如果它返回多个元素,则可以使用其他标准来缩小范围

$(function(){
    var search = 'foo';
    $("table tr td").filter(function() {
        return $(this).text() == search;
    }).parent('tr').css('color','red');
});

Will turn the text red for rows which have a cell whose text is 'foo'.对于具有文本为“foo”的单元格的行,会将文本变为红色。

This will search text in all the td's inside each tr and show/hide tr's based on search text这将在每个 tr 内的所有 td 中搜索文本,并根据搜索文本显示/隐藏 tr

 $.each($(".table tbody").find("tr"), function () {                              

                if ($(this).text().toLowerCase().replace(/\s+/g, '').indexOf(searchText.replace(/\s+/g, '').toLowerCase()) == -1)
                    $(this).hide();
                else
                    $(this).show();
 });
   <input type="text" id="text" name="search">
<table id="table_data">
        <tr class="listR"><td>PHP</td></tr>
        <tr class="listR"><td>MySql</td></tr>
        <tr class="listR"><td>AJAX</td></tr>
        <tr class="listR"><td>jQuery</td></tr>
        <tr class="listR"><td>JavaScript</td></tr>
        <tr class="listR"><td>HTML</td></tr>
        <tr class="listR"><td>CSS</td></tr>
        <tr class="listR"><td>CSS3</td></tr>
</table>

$("#textbox").on('keyup',function(){
        var f = $(this).val();
      $("#table_data tr.listR").each(function(){
            if ($(this).text().search(new RegExp(f, "i")) < 0) {
                $(this).fadeOut();
             } else {
                 $(this).show();
            }
        });
    });

Demo You can perform by search() method with use RegExp matching text 演示您可以通过 search() 方法使用 RegExp 匹配文本执行

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

相关问题 jQuery:查找包含两个具有特定文本的单元格的表行 - jQuery : find table row containing two cells that have specific text 如何使用jQuery选择器选择包含特定链接的表行 - How to Use jQuery Selector to Select Table Row Containing a Specific Link jQuery表找到最接近的tr包含并获取td文本 - jquery table find closest tr containing and get td text 在表中,将属性添加到列中包含包含特定文本的单元格的单元格 - In a table, add property to a cell in a column which include a cell containing a specific text 如何使用包含jQuery中datepicker函数的单元格向Javascript表添加行? - How do you add a row to a Javascript table with a cell containing the datepicker function from jQuery? jQuery找到上一个具有特定类单元格的表行 - jQuery find previous table row that has a cell with a specific class jQuery在同一行上按类名称查找包含Checkbox的单元格 - Jquery find cell containing Checkbox by class name on same row 使用Jquery如何找到包含具有name属性的元素的表单元格 - Using Jquery how can I find a table cell containing an element with the name property 表:每个单元格链接到包含列和行数据的URL - Table: each cell links to url containing column and row data 在jQuery中使用包含该文本的新div元素查找并替换特定文本 - Find and replace specific text with new div element containing that text in jQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM