1

これは比較的単純なようです。jQueryの構文に困惑しています。

基本的に私はこの形を取りたいです:

<div class="me_signup">
  <input type="text" name="referral[0][name]" id="referral_0_name">
  <br>
  <input type="text" name="referral[0][email]" id="referral_0_email">
</div>

そして、ボタンでそれを複製し、変数番号を増やします。

$(".add_another_button").click(function(){
    ...
};
4

2 に答える 2

7

このようなもの?

$(".add_another_button").click(function() {
    var $newdiv = $(".me_signup:last").clone(true);
    $newdiv.find('input').each(function() {
        var $this = $(this);
        $this.attr('id', $this.attr('id').replace(/_(\d+)_/, function($0, $1) {
            return '_' + (+$1 + 1) + '_';
        }));
        $this.attr('name', $this.attr('name').replace(/\[(\d+)\]/, function($0, $1) {
            return '[' + (+$1 + 1) + ']';
        }));
        $this.val('');
    });
    $newdiv.insertAfter('.me_signup:last');
});
于 2010-11-02T15:41:14.993 に答える
0

そのすべてのhtmlを変数に入れ、.append()メソッドを使用してこれを特定のDOM要素に追加できます。また、これを複製する場合は、必ず「id」を「class」に変更してください。idは各ページで一意である必要があるためです。 公式ドキュメントの.append()に関する詳細

于 2010-11-02T15:44:53.857 に答える