jQuery 可拖放问题
我遇到了与 jQuery 可拖动和可放置相关的问题。这是我想做的事情的描述。
第一:我有两个 div。一个是
,另一个是
。 “container”有 30 个可拖放到“selected”中的
。下面是代码:<div id="selected">
<ul class="sortable-list">
</ul>
</div>
<div id="container">
<ul class="sortable-list">
<li>1</li>
<li>2</li>
<li>....</li>
<li>29</li>
<li>30</li>
</ul>
</div>
第二: 我想允许从“container”到“selected”div 的任意 10 个
。如果有人尝试添加第 11 个
即 $("#container li").draggable({ revert: true });
这是 javascript 代码。
$(document).ready(function(){
var total = 0;
$("#selected").droppable({
drop: function() {
total = $("#selected li").length;
//alert(total);
if (total >= 10) {
$("#container li").draggable({ revert: true });
} else {
//$("#container li").draggable({ revert: false });
}
}
});
});
这工作正常。
第三:但是当我将
从“selected”拖到“container”时,“selected”div 将只有 9 个
。因此,在这种情况下,稍后用户应该能够将另一个
if (total >= 10 )
条件,我尝试拖放到“选定”中的所有
谁能帮我解决这个问题吗?请...
I am having an issue related jQuery draggable and droppable. Here is description something what I want to do.
First: I have two divs. One is <div id="selected">
and another is <div id="container">
. "container" has 30 <li>
which are draggable and droppable into "selected". Here is code:
<div id="selected">
<ul class="sortable-list">
</ul>
</div>
<div id="container">
<ul class="sortable-list">
<li>1</li>
<li>2</li>
<li>....</li>
<li>29</li>
<li>30</li>
</ul>
</div>
Second: I want to allow any 10 <li>
s from "container" to "selected" div. If someone tries to add 11th <li>
, then it must not allow user to it. That is the 11th <li>
that is going to be inserted into "selected" must be reverted using jQuery draggable option revert.
i.e. $("#container li").draggable({ revert: true });
Here is javascript code for that.
$(document).ready(function(){
var total = 0;
$("#selected").droppable({
drop: function() {
total = $("#selected li").length;
//alert(total);
if (total >= 10) {
$("#container li").draggable({ revert: true });
} else {
//$("#container li").draggable({ revert: false });
}
}
});
});
This is working fine.
Third: But when I drag an <li>
from "selected" to "container", the "selected" div will have only 9 <li>
s. So in this situation, later on user should be able to add another <li>
into "selected" div from "container" div. But unfortunately it is not working. All the <li>
s I try to drag and drop into "selected" are being reverted due to if (total >= 10 )
condition.
Can anyone help me to solve this out? Please...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议使用可排序,因为它在某种程度上与两个 div 上的可拖动和可放置相同。
对于可排序来说并不是很困难,有一个接收事件。观察这个事件,统计#selected里面的li,如果超过允许的数量,就可以取消当前的排序。
小提琴: http://jsfiddle.net/doktormolle/K7kDz/
I would suggest to use sortables, because its somehow the same like draggables and droppables on both div's.
It's not very diffcult with sortables, there is an receive-event. Observe this event and count the li's inside #selected, if there are more than allowed, you are able to cancel the current sorting.
Fiddle: http://jsfiddle.net/doktormolle/K7kDz/
first - 我相信你应该使 UL 可删除而不是 DIV,否则你的 LI 最终会出现在 UL 之外(你可以设置 UL 的样式来填充整个 DIV,所以它仍然会触发当元素位于其上方时可删除部分)
第二和第三 - 您将设置恢复为 false 的 ELSE 部分注释掉...是仅针对此示例还是您忘记了注释那里? (这就是它不起作用的原因)
还有 - 你是否尝试过 Firebug 来检查你是否真的不这样做一旦您将这些 LI 拖出,它们就在选定的 DIV 中吗?
first - I believe you should be making the UL droppable instead of the DIV, otherwise you LIs will end up outside the UL (you can style the UL to fill the whole DIV, so it will still trigger the droppable part when an element is above it)
second and third - you have the ELSE part where you're setting revert to false commented out... is that just for this example or did you forget the comment there? (which would be the cause why it doesn't work)
also - have you tried Firebug to check you REALLY don't have those LIs in the selected DIV once you drag them out?