jquery droppable accept

2019-01-31 12:51发布

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 div a and div b in accept condition. How can I write I through a function?

8条回答
Rolldiameter
2楼-- · 2019-01-31 13:43

This is the solution that I use:

... accept: '#diva,#divb', ...
查看更多
Bombasti
3楼-- · 2019-01-31 13:47

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.

查看更多
登录 后发表回答