我正在使用 jquery ui 拖放代码。删除后,将执行 getJSON 请求以检查新数据并更新数据库。这工作正常,直到我的后端返回错误,因为我无法从匿名函数中取消删除。
如果出现错误,后端会返回如下所示的 json:
{"result":0}
这是处理drop的代码:
$('.droppable').droppable({
drop: function(event, ui) {
$.getJSON('/roster/save', 'location_id=1', function (){
if (data.result==0) {
// now the drop should be cancelled, but it cannot be cancelled from the anonymous function
}
});
// if data was available here I could check the result and cancel it
if (data.result==0)
return false; // this cancels the drop
// finish the drop
ui.draggable.appendTo($('ul', this));
}});
我希望这有点清楚。有任何想法吗?:)