jquery clone form fields and increment id

前端 未结 4 2203
抹茶落季
抹茶落季 2020-11-27 03:37

I have a block of form elements which I would like to clone and increment their ID\'s using jQuery clone method. I have tried a number of examples but a lot of them only clo

4条回答
  •  感情败类
    2020-11-27 04:20

    Add data attribute to the input to get the field name, increment the value with variable.

    html :

    
       
      
    +

    and put this javascript function

    var rowNum = 1;

    var InsertFormRow = function(row, ptable, form)
    {
        nextrow = $(row).clone(true).insertAfter(row).prev('#' + ptable + ' tbody>tr:last');
        nextrow.attr("id", rowNum);
        nextrow.find("input").each(function() {
            this.name =  $(this).data("origin") + "_" + rowNum;
            this.id =  $(this).data("origin") + "_" + rowNum;
        });
        rowNum++;     
    }
    

提交回复
热议问题