1

我刚刚开始这样做,但我想知道如何采用下面非常简单的示例并进行修改,以便如果可拖动的 div 已经放置在可放置的 div 上,它不会触发警报。

      <script type="text/javascript">
          $(function () {
              $("#draggable-1").draggable();
              $("#draggable-2").draggable();


              $("#droppable").droppable({
                  drop: function (event, ui) {
                      var currentId = $(ui.draggable).attr('id');

                      if (currentId == "draggable-1") {
                          $(this)

                          // would like to prevent this if draggable is already dropped!
                          alert("Adding Item #1.")


                      } else {
                          $(this)
                          alert("Adding Item #2.")
                      }
                  }
              });
          });
      </script>
    <table width="100%"><tr><th>Draggable</th><th>Order Section</th><tr><td>
    <div id="draggable-1">
        <p>Item #1</p>
    </div>

    <div id="draggable-2">
        <p>Item #2.</p>
    </div>

    <div id="droppable">
        <p>Add Your Item</p>  
    </div>
4

2 回答 2

1

一种对我有用的解决方案:

var wasInDrop = false;
var finishInDrop = false;
$("#draggable_1").draggable({
    start:  function(event, ui){
            if (! finishInDrop){
                wasInDrop = false;
            }
            finishInDrop = false; //will be set to true if landing in drop.
        },
    stop:     function(event, ui){
            if (finishInDrop && !wasInDrop){
                          alert("Adding Item #1.");
            }
            if (finishInDrop){ wasInDrop=true;}

        }
    });

$("#droppable").droppable({
    drop: function (event, ui) {
        $(this)
        var currentId = $(ui.draggable).attr('id');
        if (currentId == "draggable_1") {
            finishInDrop = true; //for this time.
        }
    });

等等。这个想法是可拖动的会触发警报,如果:

  1. 它在 droppable 内完成。
  2. 它不是在 droppable 内部开始的。
于 2012-07-16T20:45:08.857 回答
1

检查这个小提琴:http: //jsfiddle.net/100thGear/XfUvS/

位置有点问题,但达到了预期的效果。让我知道这是否有帮助!

于 2012-07-16T22:47:52.323 回答