dongxing5525 2014-03-19 08:11
浏览 83

jQuery检查是否已经“删除”了正确的元素

So I have got for example 10 different "draggable" boxes and 2 "droppable" areas, 5 boxes will go into droppable 1 and then the other 5 will go into droppable 2.

If box 1 tries to go into droppable 2 it shouldn't work.

It's almost like a quiz, need to match up the correct boxes(draggable) with the correct answers(droppable).

I am struggling to find a way to do it, I can get the elements draggable and be able to drag them to any box but how can I validate if it's the correct one? Could I put some sort of value on the element?

Here's a example of how far I have got so far.... The manin website is all done in PHP so i'll be using PHP to generate the "boxes" text.

jQuery

      $(function () {
      $("#draggable,#draggable2").draggable();
      $("#droppable").droppable({
          drop: function (event, ui) {
              $(this)
                .find("p"),
                alert();
          }
      });
  });

HTML

<div id="draggable" class="ui-widget-content">
<p>Drag me to my target</p>
</div>
<div id="draggable2" class="ui-widget-content">
<p>2</p>
</div>

<div id="droppable" class="ui-widget-header">
<p>Drop here</p>
</div>
<div id="droppable" class="ui-widget-header">
<p>Drop here</p>
</div>
  • 写回答

1条回答 默认 最新

  • doushai7225 2014-03-19 08:18
    关注

    You could add some custom attributes to the draggabl and droppable html elements, and when you drop the element, you check that the value on the draggable and on the droppable is the same

    <div id="draggable" class="ui-widget-content" myValue="question1">
    <p>Drag me to my target</p>
    </div>
    
    <div id="droppable" class="ui-widget-header" myValue="question1">
    <p>Drop here</p>
    </div>
    

    Edit: added js (not tested)

    $(function () {
      $("#draggable,#draggable2").draggable();
      $("#droppable").droppable({
          drop: function (event, ui) 
         {
                 if($(this).attr("myValue") == ui.attr("myValue"))
                 {
                      alert("OK");
                 }
         }
      });
      });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决