1

元は 1 行の入力フィールドを持つテーブルを含むフォームがあります。ユーザーは [新しい行] ボタンをクリックして、入力フィールドが空の別の行を取得できます。これにより、すべての行に複数の「新しい行」ボタンのコピーが残ります。最近作成された「新しい行」ボタン (つまり、入力フィールドの最後の行にあるボタン) 以外はすべて削除したいと思います。

サンプルの JSFiddle を次の場所にセットアップしました。

http://jsfiddle.net/fmdataweb/vRe9v/2/

新しく作成されたボタンを新しい行に残しながら、クリックされたボタンを削除する既存の js に追加できるものはありますか? Javascript は次のとおりです。

var table = $( '#nextYear' )[0];

$( table ).delegate( '#button2', 'click', function () {
    var thisRow = $( this ).closest( 'tr' )[0];
   $( thisRow ).clone().insertAfter( thisRow ).find( 'input:text' ).val( '' );
});

</p>

4

3 に答える 3

1

DOM から一致した要素のセットを削除する jQuery remove()を使用できます。

ボタンクリックイベント内にいるあなたの場合this、DOMのボタンオブジェクトを参照するものです。

jQuerythisオブジェクトに変換し、次のようにremove()を呼び出します。

$(this).remove();

完全な新しいコード:

var table = $('#nextYear')[0];

$(table).delegate('#button2', 'click', function() {
    var thisRow = $(this).closest('tr')[0];
    $(thisRow).clone().insertAfter(thisRow).find('input:text').val('');

    $(this).remove(); // remove button that was clicked
});​ 

デモを見る

于 2012-08-15T11:52:38.097 に答える
1

$(this).remove();コントロールのイベントで jQuery $(this) を使用すると、イベントが発生するコントロールが表されます。remove()クリックされているボタンを削除するメソッドを使用できますdom

ライブデモ

var table = $('#nextYear')[0];

$(table).delegate('#button2', 'click', function() {

    var thisRow = $(this).closest('tr')[0];
    $(thisRow).clone().insertAfter(thisRow).find('input:text').val('');
    $(this).remove();
});​
于 2012-08-15T11:53:48.263 に答える