我在“可排序”中获取被拖动项目的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>
答案 0 :(得分:10)
获取可拖动项目ID:
var drag_id = $(ui.item).attr("id");
答案 1 :(得分:2)
您可以通过
获取IDreceive: 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
}
答案 2 :(得分:0)
如果我理解你想做什么我认为你应该这样做:
var drag_id = $(event.target).attr('id')
答案 3 :(得分:0)
试试这个家伙,
receive: function(e, ui) {
var drag_id = ui.draggable.attr("id");
alert('draggable id: ' + drag_id);
}