Javascript 添加\删除行时如何更改行号
我已经在我的表中集成了添加行和删除行功能。如果我添加第二行,第二行的编号也是1。我需要根据每次添加和删除来更改每行的编号,如1、2等 我用了小提琴上的密码Javascript 添加\删除行时如何更改行号,javascript,jquery,Javascript,Jquery,我已经在我的表中集成了添加行和删除行功能。如果我添加第二行,第二行的编号也是1。我需要根据每次添加和删除来更改每行的编号,如1、2等 我用了小提琴上的密码 如何做到这一点?任何帮助都将不胜感激。谢谢。例如,您可以添加调用重新计算功能,如下所示: function recalculate() { var i = 1; $(".numberRow").each(function() { $(this).find("strong").text(i++);
如何做到这一点?任何帮助都将不胜感激。谢谢。例如,您可以添加调用
重新计算
功能,如下所示:
function recalculate() {
var i = 1;
$(".numberRow").each(function() {
$(this).find("strong").text(i++);
});
}
其中numberRow
是td上的css类,其中存储行数
我在您的中添加了这个函数,例如,您可以添加call
recreact
函数,如下所示:
function recalculate() {
var i = 1;
$(".numberRow").each(function() {
$(this).find("strong").text(i++);
});
}
其中numberRow
是td上的css类,其中存储行数
我在你的中添加了这一点,你的代码中缺少了很多东西。我把一切都调好了 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();
}
});
现场演示:
$("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();
}
});
快乐编码:)您的代码中缺少很多东西。我把一切都调好了 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();
}
});
现场演示:
$("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();
}
});
快乐编码:)您可以添加以下内容:
var renum = 1;
$("tr td strong").each(function() {
$(this).text(renum);
renum++;
});
看这个
您的代码中有很多错误-例如,所有id
都需要唯一,您应该修复这些错误。您可以添加以下内容:
var renum = 1;
$("tr td strong").each(function() {
$(this).text(renum);
renum++;
});
看这个
代码中有很多错误-例如,所有id
都必须是唯一的,您应该解决这个问题