根据另一个单元格值从一行中获取单元格值

时间:2015-08-18 06:09:56

标签: javascript jquery html

enter image description here

我希望得到一个特定名字的年龄,假设我想要使用jquery来获得Garrett Winters的年龄。记录可以在table.i的任何一行。我必须搜索整个表并在变量中获得相应的年龄..

我想在列名称中搜索特定值并获得相应的年龄

<table id="table1" border="1" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Age</th>
                <th>Start date</th>
                <th>Status</th>
            </tr>
        </thead>

        <tfoot>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Age</th>
                <th>Start date</th>
                <th>Status</th>
            </tr>
        </tfoot>

        <tbody>
            <tr>
                <td>Tiger Nixon</td>
                <td>System Architect</td>
                <td>Edinburgh</td>
                <td>61</td>
                <td>2011/04/25</td>
                <td>CNF</td>
            </tr>
            <tr>
                <td>Garrett Winters</td>
                <td>Accountant</td>
                <td>Tokyo</td>
                <td>63</td>
                <td>2011/07/25</td>
                <td>CNF</td>
            </tr>
            <tr>
                <td>Ashton Cox</td>
                <td>Junior Technical Author</td>
                <td>San Francisco</td>
                <td>66</td>
                <td>2009/01/12</td>
                <td>CNF</td>
            </tr>
            <tr>
                <td>Cedric Kelly</td>
                <td>Senior Javascript Developer</td>
                <td>Edinburgh</td>
                <td>22</td>
                <td>2012/03/29</td>
                <td>TMP</td>
            </tr>
            <tr>
                <td>Airi Satou</td>
                <td>Accountant</td>
                <td>Tokyo</td>
                <td>33</td>
                <td>2008/11/28</td>
                <td>CNF</td>
            </tr>
            <tr>
                <td>Brielle Williamson</td>
                <td>Integration Specialist</td>
                <td>New York</td>
                <td>61</td>
                <td>2012/12/02</td>
                <td>TMP</td>
            </tr>

        </tbody>
    </table>

我是jquery的新手。帮帮我

2 个答案:

答案 0 :(得分:3)

你可以这样做。这个对我有用。 Demo

$(document).ready(function(){
   var nameToSearch ="Tiger Nixon";
   $('table tr').each(function(){
        if($(this).find('td').eq(0).text() == nameToSearch)
            alert("Age of "+nameToSearch+" is "+$(this).find('td').eq(3).text());         
   });
});

我希望它可以帮到你。

答案 1 :(得分:3)

在jquery中使用:contains Psudeo选择器。获得 Garrett Winters

年龄
var serachName = 'Garrett Winters';
$("table tbody tr td:contains("+serachName+")").parent().find('td:eq(3)').text()

Fiddle

相关问题