jquery – 如何在单击表格中的按钮时查找rowindex

栏目: jQuery · 发布时间: 6年前

内容简介:翻译自:https://stackoverflow.com/questions/14112721/how-to-find-rowindex-when-clicked-a-button-in-table

我想找到点击按钮的行.

<table>
  <tr>
    <td>foo 1</td>
    <td><input  type="button" value="Remove" id="remove1"/> </td>
  </tr>
  <tr>
    <td>foo 2 </td>
    <td><input  type="button" value="Remove" id="remove2"/> </td>
  </tr>
</table>

我的表结构如上所述.通常我可以使用buttonid获取行索引.但是如果我删除一行(tr),另一行索引会发生变化.例如:

如果我用jQuery删除第一行,第二行索引更改为0然后我不能使用按钮的id. (删除 – 2)

好吧,我认为我必须使用父功能,但它不起作用.

var elem = $('#remove2');
alert(elem.parent()[0].sectionRowIndex);

我尝试了这个,但没有用.我需要在行中单击按钮的行索引.

我希望我解释了我的问题.

试试这个:
$("table tr input").on('click', function(e){
   alert($(this).closest('td').parent()[0].sectionRowIndex);
});​

FIDDLE

翻译自:https://stackoverflow.com/questions/14112721/how-to-find-rowindex-when-clicked-a-button-in-table


以上所述就是小编给大家介绍的《jquery – 如何在单击表格中的按钮时查找rowindex》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Algorithms

Algorithms

Robert Sedgewick、Kevin Wayne / Addison-Wesley Professional / 2011-3-19 / USD 89.99

Essential Information about Algorithms and Data Structures A Classic Reference The latest version of Sedgewick,s best-selling series, reflecting an indispensable body of knowledge developed over the ......一起来看看 《Algorithms》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试