Javascript 如何在表中找到行的索引

Javascript 如何在表中找到行的索引,javascript,jquery,Javascript,Jquery,我有一个html表格,每行有一个按钮。当我点击该按钮时,我想得到该行的索引。在我的代码中,我得到了索引值,但它以1开始,而不是0。在其他示例中,我看到行索引从值0开始,但在我的示例中,它从1开始。谁能帮我一下我哪里做错了 这是我的桌子 <div class="table-style table-municipality table-edit-community-view"> <table id="sum_table"> <tr class="

我有一个html表格,每行有一个按钮。当我点击该按钮时,我想得到该行的索引。在我的代码中,我得到了索引值,但它以1开始,而不是0。在其他示例中,我看到行索引从值0开始,但在我的示例中,它从1开始。谁能帮我一下我哪里做错了

这是我的桌子

<div class="table-style table-municipality table-edit-community-view">
    <table id="sum_table">
        <tr class="titlerow">
            <th>S.N.</th>
            <th>Community</th>
            <th>Address</th>
            <th>Area</th>
            <th>Estimated</th>
            <th>Total</th>
            <th>Action</th>
        </tr>
        <? 
        $sn = 1;
        while($result= mysql_fetch_row($res))
        {
            ?>
            <tr id="<?php echo $result[0];?>">
                <td align="center"><? echo $sn++; ?></td>
                <td align="center"><? echo $result[1] ?></td>
                <td align="center"><? echo $result[2] ?></td>
                <td align="center" class="rowDataSd"><? echo $result[3] ?></td>
                <td align="center" class="rowDataSd"><? echo $result[4] ?></td>
                <td align="center" class="rowDataSd"><? echo $result[5] ?></td>
                <td>
                    <button class="test">Test</button>
                </td>                       
            </tr>
            <?
        }
         ?>
    </table>
</div>
函数告诉您

$(".test").click(function(){
    console.log("name: ", $(this).closest('td').parent().index());
});
还要注意的是,您可能只需要使用.closest'tr'而不是.closest'td'。parent

但是请注意,标题行与数据行位于同一父行中,因此将占据index=0位置。如果要避免这种情况,请将其放在自己的thead中,并将数据行放在tbody中:

无论如何,使用ad和tbody通常是最佳实践。

$.test.clickfunction{ console.logname:,$this.closest'tbody tr'。索引; }; S.N。 社区 住址 地区 估计的 全部的 行动 1. 1. 1. 1. 1. 1. 测验 2. 2. 2. 2. 2. 2. 测验
您得到的索引以1开头,因为在表的开头有一个tr元素作为标题。您可以从返回的索引中选择-1来获取以0开头的索引

$(".test").click(function(){
  console.log("name: ", $(this).closest('tr').index()-1);
});  
或在所需行(不包括标题行)的集合中查找当前行索引:

$(".test").click(function(){
  console.log("name: ", $('#sum_table tr:not(.titlerow)').index($(this).closest('tr')));
}); 

是的,我也尝试过tr,但我得到了相同的结果。我想要索引从0开始的结果。你的解决方案和我的解决方案都给了我来自1@nas:否,它给出的索引以0开头。如果要排除标题行,请从标题行中减去一行或将其放入一个字段中。我已经更新了答案来说明如何。在你的例子中,结果是1和2。我要0和1@nas因为标题行是0。。你可以把它分组。只需添加thead或tbodyI正在为临时解决方案执行-1。你的第二种方法帮助了我。非常感谢。
<div class="table-style table-municipality table-edit-community-view">
    <table id="sum_table">
        <thead><!-- *** Note -->
            <tr class="titlerow">
                <th>S.N.</th>
                <th>Community</th>
                <th>Address</th>
                <th>Area</th>
                <th>Estimated</th>
                <th>Total</th>
                <th>Action</th>
            </tr>
        </thead><!-- *** Note -->
        <tbody><!-- *** Note -->
            <? 
            $sn = 1;
            while($result= mysql_fetch_row($res))
            {
                ?>
                <tr id="<?php echo $result[0];?>">
                    <td align="center"><? echo $sn++; ?></td>
                    <td align="center"><? echo $result[1] ?></td>
                    <td align="center"><? echo $result[2] ?></td>
                    <td align="center" class="rowDataSd"><? echo $result[3] ?></td>
                    <td align="center" class="rowDataSd"><? echo $result[4] ?></td>
                    <td align="center" class="rowDataSd"><? echo $result[5] ?></td>
                    <td>
                        <button class="test">Test</button>
                    </td>                       
                </tr>
                <?
            }
             ?>
        </tbody><!-- *** Note -->
    </table>
</div>
$(".test").click(function(){
  console.log("name: ", $(this).closest('tr').index()-1);
});  
$(".test").click(function(){
  console.log("name: ", $('#sum_table tr:not(.titlerow)').index($(this).closest('tr')));
});