jquery droppable accept

后端 未结 8 2160
轻奢々
轻奢々 2020-12-13 07:02

Can anyone tell me how I can write a function in accept condition and then how does it finds out that what to accept and what not to accept. For example, I want to accept

8条回答
  •  天命终不由人
    2020-12-13 07:31

    In addition to Alex answer which is the best answer I found on this question, I like to add, that if you want to check for matching attributes between droppable and draggable, you can use the keyword this to access the droppable within your accept function. Here is a small example I recently used:

    accept : function (draggable) {
      var id_group_drop, id_group_drag;
    
      //get the "id_group" stored in a data-attribute of the draggable
      id_group_drag = $(draggable).attr("data-id-group");
    
      //get the "id_group" stored in a data-attribute of the droppable
      id_group_drop = $(this).parent().attr("data-id-group");
    
      //compare the id_groups, return true if they match or false otherwise
      return id_group_drop == id_group_drag;
    }
    

    So the draggable is only accepted, when it's id_group (remember, just an example) matches with the id_group of the droppable, otherwise the draggable would be reverted. I think this could be a very common use-case, maybe this will help someone.

提交回复
热议问题