jquery droppable -> avoid multiple drops of the same object

前端 未结 2 1461
不知归路
不知归路 2021-01-14 19:23

I have a container with different draggable -elements and there is a list of some \"target\" divs, where the user can drop the draggable elements.

Example: Imagine,

2条回答
  •  灰色年华
    2021-01-14 19:55

    First, you should make sure to never have two elements with the same id in the page. So when dropping, you want to change the id in some manner, e.g.:

    $('#doclist').droppable({
      drop: function( event, ui ) {
        tag=ui.draggable;
        tag.clone().attr("id", "copy-" + tag.attr("id")).appendTo( this );
      }
    });
    

    Next, indeed you could use accept and checking the DOM. Don't worry, I don't think it will be too resource intensive. Something like:

    $('#doclist').droppable({
      drop: function( event, ui ) {
        tag=ui.draggable;
        tag.clone().attr("id", "copy-" + tag.attr("id")).appendTo( this );
      },
      accept: function(draggable) {
        return $(this).find("#copy-" + draggable.attr("id")).length == 0;
      }
    });
    

提交回复
热议问题