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

时间:2016-08-28 09:31:31

标签: javascript jquery jquery-ui jquery-ui-sortable

我正在使用jQuery sortable。我希望能够获取移动项目的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和移动的元素?

jsFiddle

1 个答案:

答案 0 :(得分:3)

实际上它有效,你只是用错误的参数调用警报;将其替换为console.log或者将这些字符串连接起来:

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

(我将,替换为+