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

ert*_*002 8 jquery html-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>
Run Code Online (Sandbox Code Playgroud)

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

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

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

var elem = $('#remove2');
alert(elem.parent()[0].sectionRowIndex);
Run Code Online (Sandbox Code Playgroud)

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

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

pal*_*aѕн 15

试试这个:

$("table tr input").on('click', function(e){
   alert($(this).closest('td').parent()[0].sectionRowIndex);
});?
Run Code Online (Sandbox Code Playgroud)

小提琴


Jai*_*Jai 5

尝试使用此:http : //jsfiddle.net/jd9N4/1/

var elem = $('input[type="button"]');
$(elem).click(function() {
   alert($(this).closest('tr').index());
   $(this).closest('tr').remove();
});
Run Code Online (Sandbox Code Playgroud)