当拖放/删除元素(ui.sortable)时,将元素的位置置于列表中 [英] Getting the position of the element in a list when it's drag/dropped (ui.sortable)

查看:275
本文介绍了当拖放/删除元素(ui.sortable)时,将元素的位置置于列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似这样的列表: http://jqueryui.com/demos/sortable

I have a sortable list like this one: http://jqueryui.com/demos/sortable

是否可以在列表中获取元素的起始和终止位置?我在谈论他们的位置号码,在列表中。

Is it possible to get the start and end position of the element in the list, when it has been moved? I'm talking about their position number, in the list.

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

For example, if I move element 2 to position 5 in the list, I'd like to assign those two numbers to variables.

我是新来的jQuery - 任何帮助将不胜感激。

I'm new to jQuery - any help would be much appreciated.

推荐答案


  • demo: http://so.devilmaycode.it/getting-the-position-of-the-element-in-a-list-when-its-drag-dropped-ui-sortable/

解决方案:

$(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);
        }
    });
});




  • NOTE: Updated to make use of jQuery data() method under advice of Alconja

这篇关于当拖放/删除元素(ui.sortable)时,将元素的位置置于列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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