本文介绍了一旦掉落的物品数达到一定限制,避免掉落的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jquery UI Draggable.我试图避免一旦接收到该项目的div达到上限后就将其丢弃.如下所示,这是我的代码.

I am using Jquery UI Draggable.I'm trying to avoid items from be dropped once that the div which will receive that items reach a limit. As you can see below, here is my code.

 $( "#grid-list" ).droppable({
        over: function(event, ui) {
          count = 0;
          $("#grid-list").find("li").each(function () {
            count++;

            if(count > $("#grid-list").data("albumlist")){
              alert("limit reached");
            }
          });
        }
      });

JS Fiddle示例: http://jsfiddle.net/r1Lnxby9/

JS Fiddle example: http://jsfiddle.net/r1Lnxby9/

推荐答案

修改,更新

尝试从html中删除ondragstart="drag(event)"draggable="true",在js中未显示drag定义;在.droppable()选项中设置tolerance:"touch";用$("> li", this).length替换count$("#grid-list").each();如果预期结果是.droppable()元素内的1元素最大值,则为>>=;在over事件内致电event.preventDefault()event.stopPropagation()

Try removing ondragstart="drag(event)" , draggable="true" from html , drag not appear defined at js; setting tolerance:"touch" at .droppable() options ; substituting $("> li", this).length for count , $("#grid-list").each() ; >= for > if expected result is 1 element maximum within .droppable() element ; call event.preventDefault() , event.stopPropagation() within over event

  $("#list-albuns, #grid-list").sortable({
    connectWith: ".connectedSortable"
  }).disableSelection();

  $("#grid-list").droppable({
    tolerance: "touch",
    over: function(event, ui) {
      if ($("> li", this).length >= $(this).data("albumlist")) {
        event.preventDefault();
        event.stopPropagation();
        alert("limit");
      }
    }
  });
ul li {
  display: inline-block;
  list-style: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js">
</script>

<ul id="list-albuns" class="connectedSortable ui-sortable">
  <li id="0" class="ui-sortable-handle">
    <input type="hidden" name="albumId[]" value="0">
    <img id="dragimg0" height="126" width="126" src="http://userserve-ak.last.fm/serve/126/51552793.png" alt="Appetite for Destruction">
  </li>
  <li id="2" class="ui-sortable-handle">
    <input type="hidden" name="albumId[]" value="2">
    <img id="dragimg2" height="126" width="126" src="http://userserve-ak.last.fm/serve/126/97176319.png" alt="Use Your Illusion II">
  </li>
  <li id="3" class="ui-sortable-handle">
    <input type="hidden" name="albumId[]" value="3">
    <img id="dragimg3" height="126" width="126" src="http://userserve-ak.last.fm/serve/126/89710469.png" alt="40 Seasons - The Best Of Skid Row">
  </li>
  <li id="5" class="ui-sortable-handle">
    <input type="hidden" name="albumId[]" value="5">
    <img id="dragimg5" height="126" width="126" src="http://userserve-ak.last.fm/serve/126/56005467.jpg" alt="Danger Danger">
  </li>
  <li id="6" class="ui-sortable-handle">
    <input type="hidden" name="albumId[]" value="6">
    <img id="dragimg6" height="126" width="126" src="http://userserve-ak.last.fm/serve/126/85595741.jpg" alt="Screw It [Italy Bonus Tracks]">
  </li>
  <li id="7" class="ui-sortable-handle">
    <input type="hidden" name="albumId[]" value="7">
    <img id="dragimg7" height="126" width="126" src="http://userserve-ak.last.fm/serve/126/90791541.jpg" alt="Detonator">
  </li>
</ul>Drop Here:
<ul id="grid-list" data-albumlist="1" class="connectedSortable" style="width:500px; height:200px;display:block; border:2px solid black;"></ul>

jsfiddle http://jsfiddle.net/r1Lnxby9/2/

jsfiddle http://jsfiddle.net/r1Lnxby9/2/

这篇关于一旦掉落的物品数达到一定限制,避免掉落的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 15:17