在初始化之前无法调用可拖动的方法

Ric*_*ges 2 javascript jquery jquery-ui

我正在尝试销毁 jQuery UI 的可拖动实例,但收到“无法调用方法”错误。

我的代码

$('table.paper tr').draggable({
    helper: 'clone',
    create: function(event, ui) {
        $('body').on('click', '[data-action="edit-ingredients"]', function(event) {
            event.preventDefault();
            $('table.paper').draggable('destroy');
        });
    },
    start: function(event, ui) {
        c.tr = this;
        c.helper = ui.helper
        $(this).hide();
    },
    drag: function(event, ui) {
        var collides = $('table.paper').overlaps($(c.helper));
        if (collides.hits.length) {
            $(c.helper).removeClass('delete');
        } else {
            $(c.helper).addClass('delete');
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

错误
Error: cannot call methods on draggable prior to initialization; attempted to call method 'destroy' http://code.jquery.com/jquery-1.11.0.min.js Line 2

在“逻辑说话”中,这个错误告诉我我的代码流不正确,因为当我尝试销毁它时 jQuery UI 的可拖动对象未启动 - 但是,正如您所看到的,我正在可拖动对象的 ' 中创建事件侦听器创建'事件。

pum*_*zzz 5

您正在使用选择器实例化可拖动对象,$('table.paper tr')然后使用不同的选择器销毁可拖动对象$('table.paper'),我认为该选择器不是可拖动元素。