我有一个可排序的列表,我想获取我在数组中拖动的对象的位置。所以我可以用 ajax 传递它并存储到我的数据库,但我不知道如何用我的 for 循环来做到这一点

这是我的代码

<script>
    $(document).ready(function () {
        var rootLimit = 8;
        $('ul.sortable').nestedSortable({
            handle: 'a',
            items: 'li',
            listType: 'ul',
            maxLevels: '3',
            toleranceElement: '> a',
            update: function (event, ui) {
                list = $(this).nestedSortable('toHierarchy', {
                    startDepthCount: 0
                });
                var page_id = ui.item.find('> a').attr('data-page-id');
                console.log(list);
                for (var i = 0; i < list.length; i++) {
                    var index = $(this).index();
                    console.log(index);
                }
                $.post(
                    '/page/updatemenu/' + page_id,
                    { list : list },
                    function (data) { }
                );
            }
        });
    });
</script>

这是我的 jsFiddle ,谢谢!

最佳答案

在您的更新功能中,

$(ui.item).index()

给你你的物品的位置;

关于javascript - 获取拖动元素数组中的位置javascript,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15087415/

10-12 12:48