【问题标题】:renumber appended table row in javascript after deleting删除后在javascript中重新编号附加的表行
【发布时间】:2015-05-18 06:42:24
【问题描述】:
    $('#mhTable').on('click', '.DeleteRow', function() 
    {
        if(confirm("Are you sure you want to delete this?")) {
        $(this).closest('tr').remove();
        count --;
        }
        else {
            return false;
        }
    });

嗨,我有这些代码来删除表格行,我的问题是当我删除数字之间的行时,例如我有 4 行并且我删除第 2 行时,行的编号将类似于1,3,4 ..但我需要它是 1,2,3 我将如何实现这一目标?谢谢你的帮助

这是我如何动态添加表格

var rowCount = $('#mhTable >tbody >tr').length;
var count = rowCount + 1;
var host = window.location.protocol + "//" + window.location.host + "/" + "\files/Icons/delete.png";
    $('#mhAddRow').click(function(e) 
    {
        $("#mhTable tr:last").after('<tr id="row' + count + '">'
        + '<td> <sup> <img class="DeleteRow" style="cursor:pointer;" id="rowDelete" name="rowDelete" height="7" width="7" src="'+host+'"> </sup> <input type="text" id="rowsID'+count+'" name="rows['+count+']" style="width:73%;text-align:center;" value="'+count+'" readOnly="readonly"> </input> </td>'

        + '<td> <input type="text" style="width:98%;" id="process" class="process" name="process['+count+']" value=""> </input> </td>'

        + '<td> <input type="text" style="width:96%;" id="PIC" class="PIC" name="PIC['+count+']" value="" > </input> </td> '

        + '<td> <input type="text" style="width:95%;text-align:right;" id="prev" class="prev" name="prev['+count+']" onkeyup="ManhourSaving();totalmhPrevious();ManhourSavingRatio();mhInMinutes();savedMHyearly();costAnnual()" onkeypress="return isNumberKey(event)" maxlength="5" value="" ></input> </td> '

        + '<td> <input type="text" style="width:95%;text-align:right;" id="expect" class="expect" name="expect['+count+']" class="expected" onkeyup="ManhourSaving();totalmhExpected();ManhourSavingRatio();mhInMinutes();savedMHyearly();costAnnual()" onkeypress="return isNumberKey(event)" maxlength="5" value="" > </input> </td> '

        + '<td> <input type="text" style="width:96%;text-align:right;" id="mhSavings" class="mhSavings" name="mhSavings['+count+']" readOnly="readonly" value="0.00" > </input> </td> '

        + '<td> <input  type="text" style="width:95%;text-align:right;" id="ratio" class="ratio" name="ratio['+count+']" readOnly="readonly" value="0.00" > </input> </td> '

        + '<td> <input type="text" style="width:95%;text-align:right;" id="PaperReducPrev" class="PaperReducPrev" name="PaperReducPrev['+count+']"onkeyup="PaperReductionPrevious();PaperReduction();PaperReductionRatio();totalPaperReduced();paperReductionyearly();costSheet()" onkeypress="return isNumberKey(event)" maxlength="5" value="" > </input> </td>'

        + '<td> <input type="text" style="width:95%;text-align:right;" id="PaperReducExpect" class="PaperReducExpect" name="PaperReducExpect['+count+']" onkeyup="PaperReductionExpected();PaperReduction();PaperReductionRatio();totalPaperReduced();paperReductionyearly();costSheet()" onkeypress="return isNumberKey(event)" maxlength="5" value="" > </input> </td>'

        + '<td> <input type="text" style="width:96%;text-align:right;" id="paperReduced" class="paperReduced" name="paperReduced['+count+']" readOnly="readonly" value="0"> </input> </td> '

        + '<td> <input type="text" style="width:95%;text-align:right;" id="PaperReducRatio" class="PaperReducRatio" name="paperReducRatio['+count+']" readOnly="readonly" value="0.00" > </input>  </td> ');

        $.post('mis.php/new_development_request/loadView_newDevelopmentRequest', {counter: count});
        count++;
    });

【问题讨论】:

  • 编号方式和位置?向我们展示您的 HTML。

标签: javascript jquery


【解决方案1】:

先删除该行,然后使用 $.each 函数更新其他 tr

看看 fiddle 得到你的结果:https://jsfiddle.net/hL625r4p/

$(document).ready(function () {
    $("button").click(function () {
       $(this).closest('tr').remove();
        $(".row-id").each(function (i){
           $(this).text(i+1);
        }); 
    });     
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border='1'>
    <tr>
        <td class='row-id'>1</td>
        <td>Row 1</td>
        <td><button>Delete</button></td>
    </tr>
    <tr>
        <td class='row-id'>2</td>
        <td>Row 2</td>
        <td><button>Delete</button></td>
    </tr>
    <tr>
        <td class='row-id'>3</td>
        <td>Row 3</td>
        <td><button>Delete</button></td>
    </tr>
    <tr>
        <td class='row-id'>4</td>
        <td>Row 4</td>
        <td><button>Delete</button></td>
    </tr>
</table>

【讨论】:

  • 先生,我已经更新了我的帖子,我已经将代码放在了如何添加表格行上。如果那是我正在做的方法怎么办?你的解决方案可行吗?
  • 是的。可能的。这是根据您的表结构的另一个小提琴:jsfiddle.net/eh57v847
  • 我想要实现的是,即使是进程名称、PIC 等也会被重新编号
  • 您好先生.. 如果我想删除我从数据库中删除的行怎么办。也有可能吗?对不起,我真的需要一些帮助。
  • @Dale,是的,可能。你可以使用 jquery-ajax 来做到这一点。
猜你喜欢
  • 2018-09-09
  • 2016-06-15
  • 1970-01-01
  • 1970-01-01
  • 2016-12-24
  • 1970-01-01
  • 2018-09-18
  • 2016-10-07
  • 2012-09-14
相关资源
最近更新 更多