在 jQuery 中,如何恢复 ajax 调用失败时的可拖动?

发布于 2024-10-21 22:11:50 字数 533 浏览 3 评论 0原文

如果 drop 上的 ajax 调用返回失败,我希望可拖动对象恢复到其原始位置。这是我想象的代码。如果在 ajax 调用正在进行时可拖动对象位于可放置对象中,那就可以了...

<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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

二智少女 2024-10-28 22:11:51

感谢您的重播@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()
如果有人发现其中有任何问题,请告诉我! :)

Thanks for your replay @Fran Verona.

I solved it this way:

<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>

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! :)

暮色兮凉城 2024-10-28 22:11:51

今天早上我在网上寻找同样的可拖动问题。

NikhilWanpal 的解决方案有效,但有时在我的情况下,恢复位置不正确,可能是因为我的可拖动元素包含在可滚动容器中。

我发现这篇文章解释了如何实现一个非常有用的未记录的函数:jQuery UI Draggable Revert Callback。

检查您在此回调中的答案(如果您可以的话)并返回“TRUE”以取消或“FASLE”以确认新位置。

$(".peg").draggable(
{
  revert: function(socketObj)
  {
     //if false then no socket object drop occurred.
     if(socketObj === false)
     {
        //revert the peg by returning true
        return true;
     }
     else
     {
        //socket object was returned,
        //we can perform additional checks here if we like
        //alert(socketObj.attr('id')); would work fine

        //return false so that the peg does not revert
        return false;
     }
  },
  snap: '.socketInner',
  snapMode: 'inner',
  snapTolerance: 35,
  distance: 8,
  stack: { group: 'pegs', min:50 },
  stop: function()
  {
     draggedOutOfSocket = false;
     alert('stop');
  }
} );

祝你今天过得愉快

I was looking on the net for the same draggable question this morning.

The NikhilWanpal's solution works but sometimes the revert position is incorrect in my case perhaps because my draggable elements are contained in a scrollable container.

I found this article which explain how to implement a very useful undocumented function : jQuery UI Draggable Revert Callback.

Check your answer in this callback (if it is possible for you) and return "TRUE" to cancel or "FASLE" to confirm the new position.

$(".peg").draggable(
{
  revert: function(socketObj)
  {
     //if false then no socket object drop occurred.
     if(socketObj === false)
     {
        //revert the peg by returning true
        return true;
     }
     else
     {
        //socket object was returned,
        //we can perform additional checks here if we like
        //alert(socketObj.attr('id')); would work fine

        //return false so that the peg does not revert
        return false;
     }
  },
  snap: '.socketInner',
  snapMode: 'inner',
  snapTolerance: 35,
  distance: 8,
  stack: { group: 'pegs', min:50 },
  stop: function()
  {
     draggedOutOfSocket = false;
     alert('stop');
  }
} );

Have a nice day

过期情话 2024-10-28 22:11:51

如果您没有搞乱可拖动元素的左侧或顶部定位,那么就像在 ajax 调用出错后重置这些 css 属性一样简单:

drop_elems.droppable({
  drop: function(event, ui) {
    $.ajax(url, {
      error: function(x, t, e) {
        ui.draggable.css({top: 0, left: 0});
      },
    });
  }
});

If you're not otherwise messing around with left or top positioning on the draggable elements, it's as simple as resetting these css properties once the ajax call errors out:

drop_elems.droppable({
  drop: function(event, ui) {
    $.ajax(url, {
      error: function(x, t, e) {
        ui.draggable.css({top: 0, left: 0});
      },
    });
  }
});
愚人国度 2024-10-28 22:11:51

尝试在开始拖动之前保存原始位置,如果拖动失败则恢复它。您可以像这样保存原始位置:

var dragposition = '';

$('#divdrag').draggable({
   // options...
   start: function(event,ui){
      dragposition = ui.position;
   }
});

$("#dropHere").droppable({
   drop: function(){
       $.ajax({
           url: 'myurl.php',
           data: 'html',
           async: true,
           error: function(){
               $('#divdrag').css({
                  'left': dragposition.left,
                  'top': dragposition.top
               });
           }
       });
   }
});

Try to save the original position before starting to drag and restore it if drops fail. You can save the original position like this:

var dragposition = '';

$('#divdrag').draggable({
   // options...
   start: function(event,ui){
      dragposition = ui.position;
   }
});

$("#dropHere").droppable({
   drop: function(){
       $.ajax({
           url: 'myurl.php',
           data: 'html',
           async: true,
           error: function(){
               $('#divdrag').css({
                  'left': dragposition.left,
                  'top': dragposition.top
               });
           }
       });
   }
});
撞了怀 2024-10-28 22:11:51

就这样重新设置一下吧。它允许您将属性顶部和左侧设置为原始位置的动画

$("#dropHere").droppable({
        drop: function(){

            if(!result){ //failed
               ui.draggable.animate(ui.draggable.data("ui-draggable").originalPosition,"slow");

            }
            else{ //instructions for success}

        }
    });

Reset it this way. it allows you to animate the property top and left to the original position

$("#dropHere").droppable({
        drop: function(){

            if(!result){ //failed
               ui.draggable.animate(ui.draggable.data("ui-draggable").originalPosition,"slow");

            }
            else{ //instructions for success}

        }
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文