jquery draggable get its content JS fired twice

本小妞迷上赌 提交于 2020-02-05 13:08:32

问题


I noticed the first time you drag it around and then cancel the dragging, the js in the dragged item will fire another time, after that, everything seems normal, any idea?


回答1:


I found the answer on the jquery ui forum!

update is firing twice because 2 lists are being update (the original, and the list it moves to). so, you can either you the receive: option or in your update you can add: this check:

if (this === ui.item.parent()[0]) {
    //do your stuff here
}

here's the thread on the jquery forum: http://forum.jquery.com/topic/sortables-update-callback-and-connectwith




回答2:


Correction, it's actually sortable not draggable.

<ul id='test'>
  <li>
    <div>
        item1
        <script type='text/javascript'>
          alert(0);
        </script>
     </div>
   </li>
   <li>
     item2
   </li>
 </ul>    
<script type = 'text/javascript'>
  $('#test').sortable({revert: true});
</script>

in the above example you will get two alert(0) popup which is undesired in my application.



来源:https://stackoverflow.com/questions/2678598/jquery-draggable-get-its-content-js-fired-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!