如何使用jQuery为表内的序列号列连续编号

问题描述:

我有一张表。看到我的表结构如何使用jQuery为表内的序列号列连续编号

<table width="100%" border="1"> 
    <tr> 
    <td width="2%"><h2># SL NO</h2></td> 
    <td width="70%"><h2>Name</h2></td> 
    <td width="15%"><h2>Edit</h2></td> 
    <td width="13%"><h2>Delete</h2></td> 
    </tr> 


    <tr id="id0"> 
    <td >1</td> 
    <td>XXXXXXX</td> 
     <td><a href="#"><img src="../images/icon_edit.gif" alt="Edit" /></a></td> 
    <td width="13%"><a href="#"><img src="../images/delete_07.png" alt="Delete" onclick="fnDeleteState(0)" /></a></td> 
    </tr> 
    <tr id="id1"> 
    <td>2</td> 
    <td>XXXXXXX</td> 
    <td><a href="#"><img src="../images/icon_edit.gif" alt="Edit" /></a></td> 
    <td%"><a href="#"><img src="../images/delete_07.png" alt="Delete" onclick="fnDeleteState(1)" /></a></td> 
    </tr> 

... .... ..

当按下删除按钮,我需要隐藏的行我已经使用这个从表中删除特定行

$("#id" + i).remove();

它的工作正常。但是序列号......我显示为错误。即当我删除第二行和第三行中的序列号仍然3我需要显示这个S 2,依此类推......

我有什么办法提前做jQuery的 感谢

+0

你能解释一下你的问题大家一个办法就明白了? (我看不到你的问题(也许我的英语不好)) – 2012-04-05 10:23:45

function fnDeleteState(i) { 
    $("#id" + i).remove(); 
    $("tr").each(function (index) { // traverse through all the rows 
     if(index != 0) { // if the row is not the heading one 
      $(this).find("td:first").html(index + ""); // set the index number in the first 'td' of the row 
     } 
    }); 
} 
+0

中使用索引+1在每个索引中使用+1 – HerrSerker 2012-04-05 11:04:11

如果我理解正确,您需要更新每行的第一个单元格,以显示行的排名。

您可以通过添加对每个内容行的一类,例如“contentrow”实现它(从标题行区分开来),然后写这样的功能:

function refreshRowIDs(){ 
    $(".contentrow").each(function(index){ 
     $(this).children("td:first").html(index); 
    }); 
} 
+0

+1在每个 – HerrSerker 2012-04-05 11:05:05

您需要重新订购序列号。删除后。您可以使用这样的事情:

function reorder(){ 
    var i = 1; 
    $('#tableid tr').each(function() { 
     $(this).find("td:first").text(i); 
     i++; 
    }); 
    }