Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/465.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 添加\删除行时如何更改行号_Javascript_Jquery - Fatal编程技术网

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

Javascript 添加\删除行时如何更改行号,javascript,jquery,Javascript,Jquery,我已经在我的表中集成了添加行和删除行功能。如果我添加第二行,第二行的编号也是1。我需要根据每次添加和删除来更改每行的编号,如1、2等 我用了小提琴上的密码 如何做到这一点?任何帮助都将不胜感激。谢谢。例如,您可以添加调用重新计算功能,如下所示: function recalculate() { var i = 1; $(".numberRow").each(function() { $(this).find("strong").text(i++);

我已经在我的表中集成了添加行和删除行功能。如果我添加第二行,第二行的编号也是1。我需要根据每次添加和删除来更改每行的编号,如1、2等

我用了小提琴上的密码


如何做到这一点?任何帮助都将不胜感激。谢谢。

例如,您可以添加调用
重新计算
功能,如下所示:

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
都必须是唯一的,您应该解决这个问题