10

でボタンをクリックした行を検索したい。

<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>

私のテーブル構造体は上記のようなものです。通常、ボタンIDを使用して行インデックスを取得できます。しかし、行(tr)を削除すると、別の行インデックスが変更されます。例えば:

jQueryで最初の行を削除すると、2番目の行のインデックスが0に変わり、ボタンのIDを使用できなくなります。(削除-2)

親関数を使わないといけないと思いますが、うまくいきません。

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

これを試しましたが、動作しません。行のボタンをクリックした行インデックスが必要です。

私の問題を説明したいと思います。

4

4 に答える 4

21

これを試して:

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

フィドル

于 2013-01-01T17:52:37.730 に答える
6

これを使用してみてください:http://jsfiddle.net/jd9N4/1/

var elem = $('input[type="button"]');
$(elem).click(function() {
   alert($(this).closest('tr').index());
   $(this).closest('tr').remove();
});
于 2013-01-01T17:52:18.187 に答える
2

あなたは必要ありませんID

$('table').on('click', 'input[value="Remove"]', function(){
  $(this).closest('tr').remove();
});
于 2013-01-01T17:50:25.223 に答える
2

行を動的に追加する場合は、次のことができます

 $(document).on('click', 'button', function () {
     var indexRow = this.parentNode.parentNode.rowIndex;
     document.getElementById("table-name").deleteRow(indexRow);
 });
于 2017-06-12T06:19:26.580 に答える