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

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

内容简介:翻译自: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》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

The Smashing Book

The Smashing Book

Jacob Gube、Dmitry Fadeev、Chris Spooner、Darius A Monsef IV、Alessandro Cattaneo、Steven Snell、David Leggett、Andrew Maier、Kayla Knight、Yves Peters、René Schmidt、Smashing Magazine editorial team、Vitaly Friedman、Sven Lennartz / 2009 / $ 29.90 / € 23.90

The Smashing Book is a printed book about best practices in modern Web design. The book shares technical tips and best practices on coding, usability and optimization and explores how to create succes......一起来看看 《The Smashing Book》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

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

HTML 编码/解码

SHA 加密
SHA 加密

SHA 加密工具