最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cloning whole form elements after clicking button - Stack Overflow

programmeradmin0浏览0评论

I have this following form

<form action="" class="form-horizontal">
<div id="wrapper">
    <div class="row-fluid">
        <div class="span6">
            <div class="control-group">
                <label class="control-label"><?=$core->l("default_m_type");?></label>
                <div class="controls">          
                    <select id="fld_default_m_type" name="fld_default_m_type[]" defaultValue="-1" class="m-wrap span10" field="fld_default_m_type" appEditor="true">
                        <?=bo_creator::render_default_m_types()?>
                    </select>
                </div>
            </div>
        </div>
        <div class="span4 checkerAlign">
            <div class="control-group">
                <div class="controls">
                    <?=$core->l("is_active");?> 
                </div>
            </div>
        </div>
        <div class="span2 checkerAlign"><input type="checkbox" name="fld_active[]" id="fld_active" editType="booleanEdit" appEditor="true"/></div>
    </div>
    <div><a href="#" id="addMore">Add Row</a></div>
</div>

The question is when user clicks Add Row button, I need to create a copy of this form elements inside

<div id="wrapper">

How can i do that?

EDIT: SOLVED

<form action="" class="form-horizontal" id="wrapper">
<div class="row-fluid">
    <div class="span6">
        <div class="control-group">
            <label class="control-label"><?=$core->l("default_m_type");?></label>
            <div class="controls">          
                <select id="fld_default_m_type" name="fld_default_m_type[]" defaultValue="-1" class="m-wrap span10" field="fld_default_m_type" appEditor="true">
                    <?=bo_creator::render_default_m_types()?>
                </select>
            </div>
        </div>
    </div>
    <div class="span4 checkerAlign">
        <div class="control-group">
            <div class="controls">
                <?=$core->l("is_active");?> 
            </div>
        </div>
    </div>
    <div class="span2 checkerAlign"><input type="checkbox" name="fld_active[]" id="fld_active" editType="booleanEdit" appEditor="true"/></div>
</div>
<a href="#" id="addMore">add</a>
</form>

In the Js part:

jQuery("#addMore").click(function(){
 var contents = jQuery("form").html();
    jQuery("#wrapper").append(contents);
});

When add is clicked it inserts form elements just as I wanted and when delete is clicked it deletes elements. Thank you for the tips guys Problem solved! Thanks guys.

I have this following form

<form action="" class="form-horizontal">
<div id="wrapper">
    <div class="row-fluid">
        <div class="span6">
            <div class="control-group">
                <label class="control-label"><?=$core->l("default_m_type");?></label>
                <div class="controls">          
                    <select id="fld_default_m_type" name="fld_default_m_type[]" defaultValue="-1" class="m-wrap span10" field="fld_default_m_type" appEditor="true">
                        <?=bo_creator::render_default_m_types()?>
                    </select>
                </div>
            </div>
        </div>
        <div class="span4 checkerAlign">
            <div class="control-group">
                <div class="controls">
                    <?=$core->l("is_active");?> 
                </div>
            </div>
        </div>
        <div class="span2 checkerAlign"><input type="checkbox" name="fld_active[]" id="fld_active" editType="booleanEdit" appEditor="true"/></div>
    </div>
    <div><a href="#" id="addMore">Add Row</a></div>
</div>

The question is when user clicks Add Row button, I need to create a copy of this form elements inside

<div id="wrapper">

How can i do that?

EDIT: SOLVED

<form action="" class="form-horizontal" id="wrapper">
<div class="row-fluid">
    <div class="span6">
        <div class="control-group">
            <label class="control-label"><?=$core->l("default_m_type");?></label>
            <div class="controls">          
                <select id="fld_default_m_type" name="fld_default_m_type[]" defaultValue="-1" class="m-wrap span10" field="fld_default_m_type" appEditor="true">
                    <?=bo_creator::render_default_m_types()?>
                </select>
            </div>
        </div>
    </div>
    <div class="span4 checkerAlign">
        <div class="control-group">
            <div class="controls">
                <?=$core->l("is_active");?> 
            </div>
        </div>
    </div>
    <div class="span2 checkerAlign"><input type="checkbox" name="fld_active[]" id="fld_active" editType="booleanEdit" appEditor="true"/></div>
</div>
<a href="#" id="addMore">add</a>
</form>

In the Js part:

jQuery("#addMore").click(function(){
 var contents = jQuery("form").html();
    jQuery("#wrapper").append(contents);
});

When add is clicked it inserts form elements just as I wanted and when delete is clicked it deletes elements. Thank you for the tips guys Problem solved! Thanks guys.

Share Improve this question edited Oct 29, 2013 at 8:18 FreshPro asked Oct 28, 2013 at 15:20 FreshProFreshPro 8733 gold badges16 silver badges35 bronze badges 1
  • glade to hear that you have figured it out, please consider marking the best answer below as "answer" to reward the people who have tried to help you. – Mohammed Swillam Commented Oct 28, 2013 at 15:53
Add a ment  | 

3 Answers 3

Reset to default 5

I think you need to duplicate the contents of (row-fluid), not the whole (Wrapper) contents, this should let you add more rows of your original form template when clicking on AddMore link.

This is an edit to the suggested solution by @user2389688:

$("#addMore").click(function(){   
    $(".row-fluid:last").clone().appendTo(".wrapper");  
});

JsFiddle Link: http://jsfiddle/tCY8v/1/

Something like this ?

$("#addMore").click(function(){
   var contents = $("form").html();
   $("#wrapper").append(contents);  
});

http://jsfiddle/tCY8v/

If I did understand your question correctly.

For example:

$('#addMore').click(function() {
    $('.row-fluid').eq(0).clone().insertBefore(this);
});
发布评论

评论列表(0)

  1. 暂无评论