在表中插入新行

我有一个问题,其中:eq()不接受计数器, n ,以将值插入到HTML中的表的新列。

 $(document).ready(function() { var $tablerow = $('table.table').find('tr'); count = 0; $tablerow.each(function(index, value){ count += 1; var $listitem = $(this); n = parseInt($listitem.index()); var $newRow = $("" + n + ""); $("table.table tr:eq(n)").append($newRow); }); }); 

HTML

 
First row
second row
third row
fourth row
fifth row
sixth row
seventh row
eighth row

按原样编写,除了将文字字符“n”写入.eq()方法之外,您所做的只不过了。 试试这个:

 $("table.table tr:eq(" + n + ")").append($newRow); 

可以尝试使用它像前一行你使用qoutes $("table.table tr:eq(" + n + ")").append($newRow);

无需在$.each this进行索引,因为您的参数显示第一个是索引。

  $(document).ready(function() { var $tablerow = $('table.table tr'); $tablerow.each(function(index,element){ /* "this" is current row*/ $(this).append("" + index+1 + "" ) }); }); 

更换

  var $newRow = $("" + n + ""); $("table.table tr:eq(n)").append($newRow);` 

 var $newRow = $("" + (n+1) + ""); $("table.table tr:eq(" + n + ")").append($newRow); 

这会奏效