jQuery 删除一个元素并对剩余元素重新编号

发布于 2024-09-12 17:38:12 字数 321 浏览 1 评论 0原文

有谁看到我的代码中的缺陷吗,因为这让我难住了!

function removeMLRow(rowNo) {
    $('#ml_organize li:eq(' + (rowNo - 1) + ')').remove();
    $($('#ml_organize li:eq(' + (rowNo) + ')').get().reverse()).each(function() {
        var newID = 'li' + ($(this).index() - 1);
        $(this).attr('id',newID);
    });
}

Does anyone see the flaw in my code here, because this one has me stumped!

function removeMLRow(rowNo) {
    $('#ml_organize li:eq(' + (rowNo - 1) + ')').remove();
    $($('#ml_organize li:eq(' + (rowNo) + ')').get().reverse()).each(function() {
        var newID = 'li' + ($(this).index() - 1);
        $(this).attr('id',newID);
    });
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鸵鸟症 2024-09-19 17:38:12

我不能根据问题确定,但我认为这就是您所追求的:

function removeMLRow(rowNo) {
    $('#ml_organize li').eq(rowNo - 1).remove();
    $('#ml_organize li').slice(rowNo -1).each(function() {
        var newID = 'li' + ($(this).index() + 1);
        $(this).attr('id',newID);
    });
}

首先,您可以使用 .eq() 而不是 :eq() 只是让事情变得更干净。然后我们使用 .slice() 来获取所有 < code>

  • 元素在我们删除的元素之后,并且仅对那些
  • 进行编号。您可以使用 :gt() (大于 - index),但 .slice() 只是修剪字符串连接(虽然速度更快,但差异无穷小)。
  • I can't say for sure based on the question, but I think this is what you're after:

    function removeMLRow(rowNo) {
        $('#ml_organize li').eq(rowNo - 1).remove();
        $('#ml_organize li').slice(rowNo -1).each(function() {
            var newID = 'li' + ($(this).index() + 1);
            $(this).attr('id',newID);
        });
    }
    

    First, you can use .eq() instead of :eq() to just make things cleaner. Then we're using .slice() to get all the <li> elements after the one we removed and are numbering only those <li>'s. You could use :gt() (greater-than-index), but .slice() just trims down on the string concatenation (and is a bit faster, infinitesimal difference though).

    夜雨飘雪 2024-09-19 17:38:12

    您确定应该使用反向吗?据我所知,您正在删除一个元素,然后重新编号回到顶部。你应该重新编号到底部还是数字颠倒过来?

    更多信息请@dave

    Are you sure you should be using reverse. from what i see you're removing an element and then renumbering back to the top. should you be renumbering to the bottom or are the numbers reversed?

    more info please @dave

    在梵高的星空下 2024-09-19 17:38:12

    尼克,你就快到了! newID 中需要 (+1) 而不是 (-1)。

    function removeMLRow(rowNo) {
        $('#ml_organize li').eq(rowNo - 1).remove();
        $('#ml_organize li').slice(rowNo - 1).each(function() {
            var newID = 'li' + ($(this).index() + 1);
            $(this).attr('id',newID);
        });
        var item_positions = $('#ml_organize').sortable('toArray');
        alert(item_positions);
    }
    

    感谢大家的帮助!

    Nick, you were ALMOST there! Needed to (+1) instead of (-1) in the newID.

    function removeMLRow(rowNo) {
        $('#ml_organize li').eq(rowNo - 1).remove();
        $('#ml_organize li').slice(rowNo - 1).each(function() {
            var newID = 'li' + ($(this).index() + 1);
            $(this).attr('id',newID);
        });
        var item_positions = $('#ml_organize').sortable('toArray');
        alert(item_positions);
    }
    

    Thanks to all for your help!

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文