Ver Mensaje Individual
  #4 (permalink)  
Antiguo 03/12/2015, 22:25
matake
 
Fecha de Ingreso: mayo-2013
Mensajes: 191
Antigüedad: 11 años
Puntos: 10
Respuesta: adicionar una variable rowCount dentro del name e id

Pues seria esto:

Código Javascript:
Ver original
  1. $(document).ready(function ($) {
  2.     // trigger event when button is clicked
  3.     $("button.add").click(function () {
  4.         // add new row to table using addTableRow function
  5.         addTableRow($("table"));
  6.         // prevent button redirecting to new page
  7.         return false;
  8.  
  9.     });
  10.  
  11.     function cambiar_nombre_id(){
  12.    
  13.         //he puesto 300 milisegundos de retraso para estar seguro que la nueva fila esta en el DOM
  14.         setTimeout(function(){
  15.             //cuento todas las filas
  16.             $.each($(table).find("tbody tr"),function(index){
  17.                
  18.            
  19.                 // get the name attribute for the input and select fields
  20.                 $(this).find("input,select").attr("name", function () {
  21.                     // break the field name and it's number into two parts
  22.                     var parts = this.id.match(/(\D+)(\d+)$/);
  23.                     // create a unique name for the new field by incrementing
  24.                     // the number for the previous field by 1
  25.                    
  26.                     //aqui solo empleo la primera parte que es el nombre
  27.                     //en vez de la segunda parte he puesto el index actual incrementando una unidad ya que empieza por 0
  28.                     return parts[1] + (index+1);
  29.                    
  30.                     // repeat for id attributes
  31.                 }).attr("id", function () {
  32.                     var parts = this.id.match(/(\D+)(\d+)$/);
  33.                     return parts[1] +  (index+1);
  34.                 });
  35.             });
  36.         },300);
  37.     }
  38.     // function to add a new row to a table by cloning the last row and
  39.     // incrementing the name and id values by 1 to make them unique
  40.     function addTableRow(table) {
  41.        
  42.         // clone the last row in the table
  43.         var $tr = $(table).find("tbody tr:last").clone();
  44.        
  45.         //llamo a la funcion para cambiar nombres e id-s despues de añadir nueva fila
  46.         cambiar_nombre_id();
  47.  
  48.  
  49.         // append the new row to the table
  50.         $(table).find("tbody tr:last").after($tr);
  51.         $tr.hide().fadeIn('slow');
  52.  
  53.         // row count
  54.         rowCount = 0;
  55.         $("#table tr td:first-child").text(function () {
  56.             return ++rowCount;
  57.         });
  58.  
  59.         // remove rows
  60.         $(".remove_button").on("click", function () {
  61.             if ( $('#table tbody tr').length == 1) return;
  62.             $(this).parents("tr").fadeOut('slow', function () {
  63.                 $(this).remove();
  64.                 //llamo a la funcion para cambiar nombres e id-s despues de borrar una fila
  65.                 cambiar_nombre_id();
  66.                 rowCount = 0;
  67.                 $("#table tr td:first-child").text(function () {
  68.                     return ++rowCount;
  69.                 });
  70.             });
  71.         });
  72.  
  73.     };
  74. });

Ahora ya esta probado ... como ya he dicho ... es algo rudimentario ... seguro habran formas mas elegantes de hacerlo pero funciona.