jQuery 可拖动恢复:function() 无法返回“无效”
因此,我希望在恢复可拖动对象时执行一段代码,因为它没有放置在可放置对象上。不幸返回“无效”;似乎不起作用,revert 的行为就像 revert:true; 有什么建议我可以解决这个问题吗?
revert: function () {
//do some extra stuff...
return 'invalid';
}
So I want a piece of code executed when a draggable is reverted because it wasn't dropped on a droppable. Unfortunately return "invalid"; doens't seem to work, revert is acting like revert:true;
Any suggestions how I can solve this?
revert: function () {
//do some extra stuff...
return 'invalid';
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
hemant 是正确的,因为
revert
不是一个事件,它是一个选项,但它接受的值不是有效/无效,而是true/false。请参阅 jquery-ui-draggable 手册,其中指出:
hemant is right insofar that
revert
is not an event, it is an option, but its accepted values are not valid/invalid but true/false.See the jquery-ui-draggable manual which states:
这是一个非常古老的问题,但我找到了一个简单的答案。
为了既运行函数又在可拖动对象中应用恢复的“有效/无效”选项,请执行以下 2 个步骤:
1) 将可拖动对象上的恢复选项设置为您想要的任何选项,如下所示:
2)使用 Draggable 作为参数,在 droppable 的接受选项上创建一个函数,并返回 true,如下所示:
在 JQuery 3.3.1 上测试。
注意:这始终运行该函数,同时保留“有效/无效”功能。要仅在特定情况下运行该函数,我的猜测是您必须修改接受函数的返回值。
This is a really old question, but I figured out a simple answer.
In order to both run a function and apply the "valid/invalid" options of a revert in a draggable, do the following 2 steps:
1) Set the revert option on your draggable object to whichever option you want, like so:
2) Create a function on the droppable's accept option, using the draggable as parameter, and return true, like so:
Tested on JQuery 3.3.1.
Note: this ALWAYS runs the function, while keeping the "valid/invalid" functionality. To run the function only on a specific case, my guess is that you would have to modify the return value of the accept function.