最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Find if table has a specific string in jquery - Stack Overflow

programmeradmin3浏览0评论

I have a html table:

Name   Length Description  x
Name1  444    Blabd        x.png
Name2  55     Blabla       x.png
Name3  11     Blaaa        x.png

table id="firsttable" img class="delete"

I have a value (from other table):

var value = $(this).closest('tr').find('td:first').html();

I want to find that value in first column of a table and delete x.png of a row, which has that value.

Something like: (with mistakes)

$('#firsttable tr td:first:contents("'+value+'")').closest('tr').find('td:last').remove();

I have a html table:

Name   Length Description  x
Name1  444    Blabd        x.png
Name2  55     Blabla       x.png
Name3  11     Blaaa        x.png

table id="firsttable" img class="delete"

I have a value (from other table):

var value = $(this).closest('tr').find('td:first').html();

I want to find that value in first column of a table and delete x.png of a row, which has that value.

Something like: (with mistakes)

$('#firsttable tr td:first:contents("'+value+'")').closest('tr').find('td:last').remove();
Share Improve this question edited Jan 22, 2012 at 22:00 Aurelio De Rosa 22.2k8 gold badges50 silver badges71 bronze badges asked Jan 22, 2012 at 21:59 LinaLina 6275 gold badges18 silver badges35 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

A few things:

  • use the :first-child selector and not :first.
  • you should use :contains and not :contents (i don't think there is even a :contents() selector - or maybe via a plugin) (see note)
  • .siblings() will find the siblings of the current element which is the first TD. You can pass a selector to limit the selection so pass :last to get oly the last one. Makes more sense than going back to the parent and find the last TD.
  • you can empty the last TD content instead of removing the td element (but you can remove it if you want)

Here's the code:

$('#firsttable td:first-child:contains("' + value + '")')
    .siblings(':last')
    .text('');

DEMO

Note:

:contains will search in text nodes id the specified value is contained, not the exact value! So if you do :contains("1") but you have a TD with value "14", it will find it also. If this is a problem you can use .filter():

$('#firsttable td:first-child').filter(function() {
        return $(this).text() === value;
    })
    .siblings(':last')
    .text('');

More info on:

  • :first and :first-child
  • :contains
  • .siblings()
发布评论

评论列表(0)

  1. 暂无评论