如何在jquery中获得移动ID和替换ID排序

How to get moved ID and replaced ID in jquery sortable?

本文关键字:ID 移动 替换 排序 jquery      更新时间:2023-09-26

我使用jQuery排序。我希望能够获得被移动项目的ID和被替换项目的ID。到目前为止,我能够获得移动元素的ID,但不是它所取代的元素。

我的代码是:
$(function () {
    $("#sortable").sortable({
        stop: function (event, ui) {
            var moved = ui.item,
                replaced = ui.item.prev();
            // if replaced.length === 0 then the item has been pushed to the top of the list
            // in this case we need the .next() sibling
            if (replaced.length == 0) {
                replaced = ui.item.next();
            }
            alert("moved ID:" + moved.attr("id"), "replaced ID:" + replaced.attr("id"));
        }
    });
});

如何获取被替换元素的ID和被移动的元素的ID ?

jsFiddle

实际上它是有效的,你只是用错误的参数调用alert;将其替换为console.log或像这样连接字符串:

alert("moved ID:" + moved.attr("id") + "replaced ID:" + replaced.attr("id"));

(我用+代替,)