在 jQuery 中,如何恢复 ajax 调用失败时的可拖动?
如果 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
感谢您的重播@Fran Verona。
我这样解决了:
想要避免任何新的全局变量,而且变量的数量也是不可预测的,因为在第一个正在进行时,即在第一个调用返回之前,可能会发生许多拖放......!
顺便说一句,对于任何寻找相同答案的人来说, .data() 不适用于所有元素,我不过,我不确定 jQuery.data() 。
如果有人发现其中有任何问题,请告诉我! :)
Thanks for your replay @Fran Verona.
I solved it this way:
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! :)
今天早上我在网上寻找同样的可拖动问题。
NikhilWanpal 的解决方案有效,但有时在我的情况下,恢复位置不正确,可能是因为我的可拖动元素包含在可滚动容器中。
我发现这篇文章解释了如何实现一个非常有用的未记录的函数:jQuery UI Draggable Revert Callback。
检查您在此回调中的答案(如果您可以的话)并返回“TRUE”以取消或“FASLE”以确认新位置。
祝你今天过得愉快
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.
Have a nice day
如果您没有搞乱可拖动元素的左侧或顶部定位,那么就像在 ajax 调用出错后重置这些 css 属性一样简单:
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:
尝试在开始拖动之前保存原始位置,如果拖动失败则恢复它。您可以像这样保存原始位置:
Try to save the original position before starting to drag and restore it if drops fail. You can save the original position like this:
就这样重新设置一下吧。它允许您将属性顶部和左侧设置为原始位置的动画
Reset it this way. it allows you to animate the property top and left to the original position