如何在$ .unbind之后重新启用/重新绑定jQuery UI Sortable?

Ste*_*eve 6 jquery jquery-ui jquery-ui-sortable

$('div.something').sortable(options)工作正常,但后来打破$('div.something').unbind();.试图重新运行$('div.something').sortable(options);$('div.something').sortable('refresh');之后$('div.something').unbind();没有帮助.

我使用$ .unbind通过从应用插件的元素中删除事件来停用/取消启动插件,但是这种技术正在产生不利影响,因为它打破了$ .sortable.关于如何重新激活可排序的任何想法?

我正在使用最新版本的jQuery和jQuery UI.

Rub*_*nte 9

在调用.sortable('destroy')之前调用.unbind()将完全删除元素中的可排序功能.

这确保在执行稍微危险的调用之前正确拆除.unbind(),这将删除元素上的所有绑定处理程序.然后,您可以重新初始化可排序功能.

// Initialization of the sortable feature
$('div.something').sortable(options);
...
// Remove the sortable feature to prevent bad state caused by unbinding all
$('div.something').sortable('destroy');
// Unbind all event handlers!
$('div.something').unbind();
...
// Re-initialize the sortable feature
$('div.something').sortable(options);
Run Code Online (Sandbox Code Playgroud)


Aru*_*hny 7

如果要停用可排序插件,我建议使用destroy方法

$('div.something').sortable(options)//Activate
....
$('div.something').sortable('destroy')//Remove the plugin functionality
....
$('div.something').sortable(options)//Reactivate
Run Code Online (Sandbox Code Playgroud)

演示:小提琴