使用 jQuery 重新编号表行的 id [英] Re-number the id's of table rows using jQuery

查看:27
本文介绍了使用 jQuery 重新编号表行的 id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下表格代码:

<tbody>
    <tr data-id='1' id='item-1'>
        <td>30-01-2014</td>
    </tr>   
    <tr data-id='19' id='item-2'>
        <td>30-01-2014</td>
    </tr>
    <tr data-id='5' id='item-3'>
        <td>30-01-2014</td>
    </tr>
    <tr data-id='39' id='item-4'>
        <td>30-01-2014</td>
    </tr>
</tbody>

和 jQuery:

$('table tbody').sortable({
    axis: 'y',
    update: function (event, ui) {
        var data = $(this).sortable('serialize');
        $.ajax({
            data: data,
            type: 'POST',
            url: 'updatePagesOrder.php'
        });
    }
});
var i = 1;
$('table > tbody  > tr').each(function () {
    $('tr').attr('id', 'item-' + i);
    i++;
});

我想要做的是在sortable函数执行后,item的id会从1到4重新排序.我曾尝试在上面编写代码,但没有奏效.

What I want to do is that after the sortable function executed the id of the items will be ordered again from 1 to 4. I have tried to code above, but it didn't work.

希望得到帮助...谢谢!

Wish for help...Thanks!

推荐答案

无需声明 i 你已经在使用 .each() 所以

No need to declare i you're already using .each() So

试试这个

$('table > tbody tr').each(function (i) {
     $(this).attr('id', 'item-' + (i + 1)); // use $(this) as a reference to current tr object
});

使用 (i+1) 因为索引从 0

这篇关于使用 jQuery 重新编号表行的 id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆