Clone in JQuery and adding unique IDs for each

前端 未结 2 533
感动是毒
感动是毒 2020-12-17 05:23

I\'m creating a simple web app, where users come in; make a selection from drop down boxes and enter some text. Once this is completed, users press a \"generate\" button whi

2条回答
  •  旧巷少年郎
    2020-12-17 06:08

    i've updated your fiddle to give the new forms unique id's

    http://jsfiddle.net/zq3AN/

    it's probably not exactly perfect, but should get you going in the right direction.

    var uniqueId = 1;
    $(function() {
         $('.addRow').click(function() {
             var copy = $("#original").clone(true);
             var formId = 'NewForm' + uniqueId;
             copy.attr('id', formId );
    
    
             $('#campaign').append(copy);
             $('#' + formId).find('input,select').each(function(){
                $(this).attr('id', $(this).attr('id') + uniqueId);
    
             });
             uniqueId++;  
         });
    });
    

    basically you copy the form, change it's id, and append it to the div. then you look form all the inputs and selects and append the same uniqueId parameter to their ids.

提交回复
热议问题