最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Copy table row and increment all name attributes - Stack Overflow

programmeradmin1浏览0评论

Let's have a table like this:

<table>
 <tr>
  <td><input type="text" name="FirstName1" /></td>
  <td><input type="text" name="LastName1" /></td>
 </tr>
 <tr>
  <td><input type="text" name="FirstName2" /></td>
  <td><input type="text" name="LastName2" /></td>
 </tr>
</table>

I want to have a button that will add new row at the end of the table with incremented name attributes so it will look like this:

<table>
 <tr>
  <td><input type="text" name="FirstName1" /></td>
  <td><input type="text" name="LastName1" /></td>
 </tr>
 <tr>
  <td><input type="text" name="FirstName2" /></td>
  <td><input type="text" name="LastName2" /></td>
 </tr>
 <tr>
  <td><input type="text" name="FirstName3" /></td>
  <td><input type="text" name="LastName3" /></td>
 </tr>
</table>

And so on.

So far I have this but it does not increment name attributes:

$("#newRowButton").click(function(){
  $("table tr:last").clone().appendTo("table");
});

Let's have a table like this:

<table>
 <tr>
  <td><input type="text" name="FirstName1" /></td>
  <td><input type="text" name="LastName1" /></td>
 </tr>
 <tr>
  <td><input type="text" name="FirstName2" /></td>
  <td><input type="text" name="LastName2" /></td>
 </tr>
</table>

I want to have a button that will add new row at the end of the table with incremented name attributes so it will look like this:

<table>
 <tr>
  <td><input type="text" name="FirstName1" /></td>
  <td><input type="text" name="LastName1" /></td>
 </tr>
 <tr>
  <td><input type="text" name="FirstName2" /></td>
  <td><input type="text" name="LastName2" /></td>
 </tr>
 <tr>
  <td><input type="text" name="FirstName3" /></td>
  <td><input type="text" name="LastName3" /></td>
 </tr>
</table>

And so on.

So far I have this but it does not increment name attributes:

$("#newRowButton").click(function(){
  $("table tr:last").clone().appendTo("table");
});
Share Improve this question asked Apr 15, 2011 at 9:23 Richard KnopRichard Knop 83.8k154 gold badges398 silver badges561 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 8
$("#newRowButton").click(function() {
  $("table tr:last")
      .clone()
      .appendTo("table")
      .find(':input')
      .attr('name', function(index, name) {
          return name.replace(/(\d+)$/, function(fullMatch, n) {
              return Number(n) + 1;
          });
      })
});

Live demo.

$("#newRowButton").click(function(){
   var trows = $("table tr:last").clone();
   trows.find('td input').attr("name",function(i,a){
   var p = new RegExp("((?:[a-z][a-z]+))(\\d+)",["i"]);
   var m = p.exec(a);
   var index = parseInt(m[1]);
   index++;
   return m[0]+index;
   });
   trows.appendTo("table");
});
var clonedRow = $("#EventType tr:last").clone();
$("input", clonedRow).attr('name', 'FirstName3'); // reset the name

you can get the name attribute and increment by 1

Reference

发布评论

评论列表(0)

  1. 暂无评论