【问题标题】:how to get the id of droppable item using sortable如何使用 sortable 获取可放置项目的 id
【发布时间】:2014-06-30 06:59:30
【问题描述】:

如何使用 sortable.我可以获取拖动项目的 ID,但无法获取要删除的项目的 ID。这是我迄今为止尝试过的代码:

$(function () {
$('#sortable').sortable({
    //to prevent items from moving around when draging
    containment: "parent",
    start: function (event, ui) {
        // get the initial position(index) of item
        var start_pos = ui.item.attr('id');
        ui.item.data('start_pos', start_pos);

   },

    update: function (event, ui) {
        var index = ui.item.index();//position of dropped
        var start_pos = ui.item.attr('id');//position of dragged
        alert(start_pos);
        alert(index);

        // Iterate over all <li> elements
        $.each($('#sortable li'), function (idx, item) {

           // $(item).html(html + ' - ' + (idx + 1));
        });
    },
    axis: 'y'
});

例如,如果我将项目从位置 1 拖动到位置 5,我想提醒 1 和 5

【问题讨论】:

    标签: jquery jquery-ui jquery-ui-sortable jquery-ui-droppable


    【解决方案1】:

    试试下面的代码:-)

    $('.selector').droppable({ drop: Drop });
    
    function Drop(event, ui) {
      var draggableId = ui.draggable.attr("id");
      var droppableId = $(this).attr("id");
    }
    

    【讨论】:

    • 我不得不使用 sortable。我尝试使用 toArray 来获取所有 ID。目的是在我更改项目位置后重新使用 ID。但我不知道如何重新排列从 toArray 获取的 ID
    猜你喜欢
    • 2018-11-30
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    • 2011-07-30
    • 1970-01-01
    • 1970-01-01
    • 2021-08-06
    • 2015-06-01
    相关资源
    最近更新 更多