简体   繁体   中英

jquery cant get drag item id in Sortable

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

<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>

要获取可拖动的商品ID:

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

You can get the ID by

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
}

try this dude,

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

如果我正确理解了您想做什么,我认为您应该执行以下操作:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM