JQuery: How to select rows from a table

后端 未结 3 1346
不知归路
不知归路 2020-12-07 03:01

I have a scenario where I would like to select rows from a table depending upon the values in td

e.g. I have a table like this


   

        
3条回答
  •  情话喂你
    2020-12-07 03:43

    $("tr").each(function (index) {
       if ($.trim($(this).find('td:eq(0)').text()) == x &&
           $.trim($(this).find('td:eq(1)').text()) == y) {
           $(this).closest('table').css('border', '1px solid red');​ // this should do it
       }
    }); 
    

    Alternatively, using .filter:

    $("tr").filter(function() {
        return $.trim($(this).find('td:eq(0)').text()) == 'John' &&
               $.trim($(this).find('td:eq(1)').text()) == 'Smith';
    }).closest('table').find('tr').css('border', '1px solid red');​
    

    Demo: http://jsfiddle.net/WhFbE/5/

提交回复
热议问题