2015-05-02 24 views
1

这样的IM建立一个发票的工具,我需要能够添加一个新行,以便添加包含姓名和ID值输入一个新的产品。添加一个新行jQuery的一个表,但应用唯一ID输入名称

我有以下JS为我完美创建新的行,但任何想法如何我可以调整,所以它在输入和选择列表上找到name =“blah”和id =“blah”并添加到结尾_1 ,_2,_3等等......每个新行都有数字。

JS:

// add new product row on invoice 
    $(".add-row").click(function() { 
     $("#invoice_table").each(function() { 
      var tds = '<tr>'; 
      jQuery.each($('tr:last td', this), function() { 
       tds += '<td>' + $(this).html() + '</td>'; 
      }); 
      tds += '</tr>'; 
      if ($('tbody', this).length > 0) { 
       $('tbody', this).append(tds); 
      } else { 
       $(this).append(tds); 
      } 
     }); 

     return false 
    }); 

回答

1

您可以使用现有的指数在$.each ...这样的:

$(".add-row").click(function() { 
     $("#invoice_table").each(function() { 
      var tds = '<tr>'; 
      jQuery.each($('tr:last td', this), function (index, element) { 
       var html = $(this).html().replace(/blah/g, 'blah_'+index); 
       tds += '<td>' + html + '</td>'; 
      }); 
      tds += '</tr>'; 
      if ($('tbody', this).length > 0) { 
       $('tbody', this).append(tds); 
      } else { 
       $(this).append(tds); 
      } 
     }); 

     return false 
    }); 
+0

林不知道,我发现这段代码在线帮助我添加它,并不确定如何适应我的需求。我对此很新。 – James

+0

将有大约6个不同的我需要添加_ +索引,我应该添加多个替换? – James

+0

我想我需要它来查找和替换,我会在JS内使用标记,但一些选择列表填充PHP。 – James

0

您可以使用计数器它

$("#invoice_table").each(function() { 
      var count=0; 
      var tds = '<tr>'; 
      jQuery.each($('tr:last td', this), function() { 
       var count=count+1; 
       tds += '<td id="blah'+counter+'" name="blah'+counter+'">' + $(this).html() + '</td>'; 
      }); 
      tds += '</tr>'; 
      if ($('tbody', this).length > 0) { 
       $('tbody', this).append(tds); 
      } else { 
       $(this).append(tds); 
      } 
     }); 

     return false 

使每次它会创建一个唯一标识符

+0

我需要查找和替换真的,因为它使用JS为标记一些sleect列表与PHP,你看我填充不能建立。 – James

相关问题