在jQuery中,如何在ajax调用失败时还原可拖动对象? [英] In jQuery, how to revert a draggable on ajax call failure?

查看:116
本文介绍了在jQuery中,如何在ajax调用失败时还原可拖动对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果放下的Ajax调用返回失败,我希望可拖动对象恢复到其原始位置.这是我想像的代码.如果在进行ajax调用时将可拖动对象放置在droppable中,就可以了...

I want the draggable to be reverted to its original position if the ajax call on drop returns a failure. Here is the code what I am imagining it to be.. It is OK if the draggable rests in the droppable while the ajax call is in process...

<script type="text/javascript">
jQuery(document).ready($){
    $("#dragMe").draggable();
    $("#dropHere").droppable({
        drop: function(){
            // make ajax call here. check if it returns success.
            // make draggable to return to its old position on failure.
        }
    });
}
</script>
<div id="dragMe">DragMe</div>
<div id="dropHere">DropHere</div>

推荐答案

感谢您重播@Fran Verona.

Thanks for your replay @Fran Verona.

我这样解决了:

<script type="text/javascript">
jQuery(document).ready($){
    $("#dragMe").draggable({
        start: function(){
        $(this).data("origPosition",$(this).position());
        }
    });
    $("#dropHere").droppable({
        drop: function(){
            //make ajax call or whatever validation here. check if it returns success.
            //returns result = true/false for success/failure;

            if(!result){ //failed
                ui.draggable.animate(ui.draggable.data().origPosition,"slow");
                return;
            }
            //handling for success..
        }
    });
}
</script>
<div id="dragMe">DragMe</div>
<div id="dropHere">DropHere</div>

想要避免任何新的全局变量,变量的数量也是不可预测的,因为在第一个调用进行时,即在第一个调用返回之前,可能发生许多拖放. 顺便说一句,对于任何寻求相同答案的人, .data()不适用于所有元素,我不过,我不确定 jQuery.data(). 让我知道是否有人发现任何错误! :)

Wanted to avoid any new global variables, also the number of variables was unpredictable as many drag-drops can happen while the first is in progress, i.e. before the 1st call returns..! BTW, for anyone looking for the same answer, .data() does not work on all elements, I am not sure about jQuery.data(), though.. Let me know if anyone finds anything wrong in this! :)

这篇关于在jQuery中,如何在ajax调用失败时还原可拖动对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆