How to get the dropped item's id on drop event

2019-04-29 05:26发布

When I drop something to jquery droppable, I want to get the dropped items' id. When I did it like this:

$("#here").droppable({
        tolerance: 'fit',
        accept: ".one",
         drop: function(){
            id = $(this).attr("id");
            alert (id);
        }
    });

it of course alerted the id of the droppable here. How can I select the id of the dropped div?

2条回答
Anthone
2楼-- · 2019-04-29 06:07

This worked for me:

   $( "#droppable" ).droppable({
          drop: function( event, ui ) {
                var draggableId = ui.draggable.attr("id");
            var droppableId = $(this).attr("id");
          }
        });
      });
查看更多
smile是对你的礼貌
3楼-- · 2019-04-29 06:11

Change your drop function to take two arguments: event, ui

function(event,ui){
    var draggable = ui.draggable;
    var id = draggable.attr("id");
}

The draggable that is being dropped is represented by ui.draggable

Found in the jquery ui docs for droppable.

查看更多
登录 后发表回答