Clone in JQuery and adding unique IDs for each

前端 未结 2 526
感动是毒
感动是毒 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 05:50

    I think you can use jQuery clone() function to implement your "add row" functionality.

    Check out the last example for giving each new row an unique id.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题