0

現在、次のコードを使用しています。要素を複製し、複製の入力を必要なものに変更します。クローンにも選択があり、その名前を別の値に変更する必要があります。同じ clone コマンドで同時に 2 つの子を実行するにはどうすればよいですか?

コード:

$(this).parent().parent()
    .append($(this).parent()
    .clone()
    .children('input').attr('name', VAL)
    .end()
);

私はこのようなものが必要です

$(this).parent().parent()
    .append($(this).parent()
    .clone()
    .children('input').attr('name', VAL)
    .children('select').attr('name', VAL2) // Where this does not target children of the input, but of the clone.
    .end()
);
4

2 に答える 2

1
$(this).parent().parent()
    .append($(this).parent()
    .clone()
    .children('input, select').attr('name', VAL)
);

または:

$(this).parent().parent()
    .append($(this).parent()
    .clone()
    .children('input').attr('name', VAL)
    .end()
    .children('select').attr('name', OtherVal) 
);

ライブデモ

于 2013-05-22T18:49:47.680 に答える
0

この答えは gdoron のおかげですが、彼は自分の答えを実際の正解に変更することを拒否しました。クローンまたは任意のオブジェクトで複数の子を編集する必要がある場合は、単純に end を使用して次の子を指定します。

$(this).parent().parent()
    .append($(this).parent()
    .clone()
    .children('input').attr('name', VAL)
        .end()
    .children('select').attr('name', VAL2) 
        .end()
);
于 2013-05-23T04:18:52.937 に答える