最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Error When Destroying Draggable After Drop - Stack Overflow

programmeradmin3浏览0评论

I am receiving an error in jQuery UI when I attempt to destroy a draggable after dropping it. I am using jQuery 1.9.1 and jQuery UI 1.10.0.

Script

$(".drag").draggable({
    revert: "invalid",
    helper: "clone"
});

$(".drop").droppable({
    accept: ".drag",
    drop: function(event,ui){
      ui.helper.remove();
      $(this).append(ui.draggable);
      $(ui.draggable).draggable("destroy");
    }
});

HTML

<div class="drag">Draggable</div>
<div class="drop">Droppable</div>

Example: /

Error Received

TypeError: $(...).data(...) is undefined

I've spent the past few hours on this with no luck. I found one similar post which did not contain a resolution. Can anyone help me out here? Thanks!

I am receiving an error in jQuery UI when I attempt to destroy a draggable after dropping it. I am using jQuery 1.9.1 and jQuery UI 1.10.0.

Script

$(".drag").draggable({
    revert: "invalid",
    helper: "clone"
});

$(".drop").droppable({
    accept: ".drag",
    drop: function(event,ui){
      ui.helper.remove();
      $(this).append(ui.draggable);
      $(ui.draggable).draggable("destroy");
    }
});

HTML

<div class="drag">Draggable</div>
<div class="drop">Droppable</div>

Example: http://jsfiddle/feDME/

Error Received

TypeError: $(...).data(...) is undefined

I've spent the past few hours on this with no luck. I found one similar post which did not contain a resolution. Can anyone help me out here? Thanks!

Share Improve this question asked Mar 19, 2013 at 16:57 Kevin BowersoxKevin Bowersox 94.5k19 gold badges163 silver badges197 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

It looks like there's a race condition in the jquery-ui draggable code where it tries to set the cursor when dragging stops. The following line is failing because the "draggable" data isn't attached to the draggable div yet when stop is called.

var o = $(this).data('draggable').options;

It's a bit of a hack but this setTimeout will fix it.

$(".drop").droppable({
    accept: ".drag",
    drop: function(event,ui){
      ui.helper.remove();
      $(this).append(ui.draggable);
      setTimeout(function() {
          $(ui.draggable).draggable("destroy");      
      }, 0);      
    }
});

I wouldn't remend you use the setTimeout hack. The proper way to really call the destroy method on demand is to remove the special class ui-draggable-dragging before calling the destroy method. So, your code would look like this:

$(".drop").droppable({
    accept: ".drag",
    drop: function(event,ui){
      ui.helper.remove();
      $(this).append(ui.draggable);
      var dragElem = $(ui.draggable);
      // this will destroy the item
      dragElem.removeClass('ui-draggable-dragging').draggable("destroy");
    }
});

Check the draggable code, to understand what's going on and why remove this class https://github./jquery/jquery-ui/blob/master/ui/draggable.js#L92

发布评论

评论列表(0)

  1. 暂无评论