如何在添加\删除行时更改行号

问题描述:

我在表中集成了添加行和删除行功能。如果我添加第二行的行数也是1.我需要更改number of each row每增加和删除像1,2等。如何在添加删除行时更改行号

我用这个代码从这个小提琴http://jsfiddle.net/MJGV2/6/

如何实现这一目标?任何帮助,将不胜感激。谢谢。

可以补充一点:

var renum = 1; 
$("tr td strong").each(function() { 
    $(this).text(renum); 
    renum++; 
}); 

看到这个Fiddle demo

代码中有很多错误 - 例如,所有id都必须是唯一的,您应该修复此问题。

例如,您可以添加通话recalculate功能,如:

function recalculate() { 
    var i = 1; 
    $(".numberRow").each(function() { 
     $(this).find("strong").text(i++);  
    }); 
} 

numberRow是TD,其中行的门店数量CSS类。

我在你jsfiddle example

添加此有很多事情在你的代码失踪。我已经调整所有功能正常

JS代码:

$("input[type='button'].AddRow").on('click', 

function() { 
if ($(this).val() == 'Delete') { 
    trSet = $(this).closest('tr').nextAll(); 
    currIndex = $(this).closest('tr').find('td span').html(); 
    $(this).closest('tr').remove(); 
    trSet.each(function() { 
     $(this).find('td span').html(currIndex); 
     currIndex++; 
    }); 
    count = currIndex - 1; 
} else { 
    var $tr = $(this).closest('tr').clone(true); 
    var $input = $tr.find('input.startdatum'); 
    ++count; 
    $tr.find('td span').html(count); 
    $(this).val('Delete'); 
    var id = 'datepicker' + count; 
    $input.attr('id', id).data('index', count); 
    console.log(count); 
    $tr.appendTo($(this).closest('table')); 
    setdatepicker(); 
} 
}); 

现场演示:

http://jsfiddle.net/MJGV2/14/

编码快乐:)