簡體   English   中英

刪除行后如何對表序列號進行重新排序

[英]How to reorder the table serial no after deleting the row

我有一張桌子,每行只有幾個按鈕。 單擊每行的刪除按鈕時,必須刪除表行。 我的問題是,當我刪除未按順序排列的SN行時。 例如:如果有5行,並且一旦我刪除了第一行,它將按2、3、4、5(而不是1、2、3、4)順序排列。

<table>
<thead>
    <tr>
        <th>S.N.</th>
        <th>Name</th>
        <th>Address</th>
        <th>Modified Date</th>
        <th>Action</th>
    </tr>
</thead>
<tbody>
    <?php 
    $sn = 1;
    while($result= mysql_fetch_row($res))
    {

        ?>
        <tr id=<?= $result[0];?>>
            <td><? echo $sn++; ?></td>
            <td><? echo $result[1] ?></td>
            <td><? echo $result[2] ?></td>
            <td><? echo $result[3] ?></td>
            <td>
                <a href="#" id="delete">Delete</a>
                <a href="#" id="edit">Edit</a>
            </td>

        </tr>

        <?php } ?>
    </tbody>
</table>

$('#delete').on('click', function(e) {
    e.preventDefault();
    var id = $(this).closest('tr').attr('id');
    $(this).closest('tr').remove();
});

首先,您不應具有相同的ID。 ID必須是唯一的。 您可以先使用class然后再使用class選擇器來定位這些元素:

$('.delete').on('click......

對於重新排序,可以在.attr回調函數中使用element的索引來基於索引設置id:

$('table tbody tr').attr('id',function(i,o){
  return i++ ;
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM