拖动/删除元素在列表中的位置(ui.sortable)

Wur*_*zer 12 javascript jquery drag-and-drop position jquery-ui-sortable

我有一个像这样的可排序列表:http://jqueryui.com/demos/sortable

是否有可能在列表移动时获取元素的开始和结束位置?我在谈论他们的位置编号,在列表中.

例如,如果我将元素2移动到列表中的位置5,我想将这两个数字分配给变量.

我是jQuery的新手 - 任何帮助都会非常感激.

Luc*_*ofi 32

解:

$(function() {
    $('ul#sortable').sortable({
        start: function(event, ui) {
            var start_pos = ui.item.index();
            ui.item.data('start_pos', start_pos);
        },
        update: function(event, ui) {
            var start_pos = ui.item.data('start_pos');
            var end_pos = ui.item.index();
            alert(start_pos + ' - ' + end_pos);
        }
    });
});
Run Code Online (Sandbox Code Playgroud)