Adding rows dynamically with jQuery

前端 未结 5 997
天命终不由人
天命终不由人 2020-11-28 02:48

I\'m building a form where I need multiple optional inputs, what I have is basically this:

Every time a user presses the plus button a new row of form inputs

5条回答
  •  旧巷少年郎
    2020-11-28 03:09

    Untested. Modify to suit:

    $form = $('#my-form');
    
    $rows = $form.find('.person-input-row');
    
    $('button#add-new').click(function() {
    
        $rows.find(':first').clone().insertAfter($rows.find(':last'));
    
        $justInserted = $rows.find(':last');
        $justInserted.hide();
        $justInserted.find('input').val(''); // it may copy values from first one
        $justInserted.slideDown(500);
    
    
    });
    

    This is better than copying innerHTML because you will lose all attached events etc.

提交回复
热议问题