jquery无法在Sortable中获取拖动项ID

时间:2021-07-27 20:33:39

i am having problem in getting the id of the dragged item in "sortable", can you please help me out in this.

我在获取“可排序”中拖动项目的ID时遇到问题,请你帮我解决一下。

<script>
$(document).ready(function(){
    $("#div1,#div2,#div3").sortable({
        revert: true,
        accept: '.draggable',
        connectWith: [".sortable_div"],
        receive: function(e, ui) { 
            var item_id = $(this).attr("id");
            var drag_id = $(ui.item).attr('id')
            alert('alert:'+item_id+' of '+drag_id);
        }
    }).disableSelection();
});
</script>


<div id="div1" class="sortable_div">
<span id="span1" class="draggable"></span>
</div>
<div id="div2" class="sortable_div">
<span id="span2" class="draggable"></span>
</div>
<div id="div3" class="sortable_div">
<span id="span3" class="draggable"></span>
</div>

4 个解决方案

#1


10  

To get draggable item id:

获取可拖动项目ID:

var drag_id = $(ui.item).attr("id");

#2


2  

You can get the ID by

您可以通过获取ID

receive: function(e, ui) { 
   var item_id = $(this).attr("id");
   //ui.draggable.attr('id') or ui.draggable.get(0).id or ui.draggable[0].id
   var drag_id = ui.draggable.attr('id');
   alert('alert:'+item_id+' of '+drag_id);
   console.log(ui.draggable); // to see the bunch of items
}

#3


0  

If i understood correctly what you want to do i think you should do something like this:

如果我理解你想做什么我认为你应该这样做:

var drag_id = $(event.target).attr('id')

#4


0  

try this dude,

试试这个家伙,

receive: function(e, ui) { 
    var drag_id = ui.draggable.attr("id");
    alert('draggable id: ' + drag_id);
}

#1


10  

To get draggable item id:

获取可拖动项目ID:

var drag_id = $(ui.item).attr("id");

#2


2  

You can get the ID by

您可以通过获取ID

receive: function(e, ui) { 
   var item_id = $(this).attr("id");
   //ui.draggable.attr('id') or ui.draggable.get(0).id or ui.draggable[0].id
   var drag_id = ui.draggable.attr('id');
   alert('alert:'+item_id+' of '+drag_id);
   console.log(ui.draggable); // to see the bunch of items
}

#3


0  

If i understood correctly what you want to do i think you should do something like this:

如果我理解你想做什么我认为你应该这样做:

var drag_id = $(event.target).attr('id')

#4


0  

try this dude,

试试这个家伙,

receive: function(e, ui) { 
    var drag_id = ui.draggable.attr("id");
    alert('draggable id: ' + drag_id);
}