Jquery 可投放的添加现场活动

Jquery Droppable add live event

我有一个 li,它可以拖放并在刷​​新页面时工作,但是当我在列表中使用 AJAX 添加 li 时,droppable事件不会附加到它。如何将 live 可放置事件附加到 li

这是我到目前为止所尝试的,

$('.main-menu.workspaces li').droppable({
  hoverClass: "ui-state-hover",
  tolerance: "pointer",
  accept: '.notification-item.subtask',
  drop: function (event, ui) {
    var workspace_id = $(event.target).data('workspace');
    var task_id = $(ui.draggable[0]).data('taskid');
    $.post('{{route('workspace.assign', $subdomain)}}', { 
      workspace: workspace_id, 
      task: task_id   
    }).done(function(data) {
    });
  }
});

感谢@Rory McCrossan 的建议,这就是我所做的并且奏效了。

(function($){
      var options = {
          hoverClass: "ui-state-hover",
          tolerance: "pointer",
          accept: '.notification-item.subtask',
          drop: function (event, ui) {
          var workspace_id = $(event.target).data('workspace');
              var task_id = $(ui.draggable[0]).data('taskid');
              var href = $('.notification-body.todotask').attr('data-assignlink');
              $.post(href,{workspace: workspace_id, task: task_id})
              .done(function(data){
                   //todo
               });
             }
           }

       $('.main-menu.workspaces li').droppable(options);


       // Attaching li to DOM using AJAX
       function attachLi(){
           $.ajax({
           url: 'store',
           method: 'POST',
           data: {
           task: searchString,
           },
           success: function (response) {
               //attaching li to DOM
               $('.task').append(response.html);

               //Reinitializing Droppable 
               $('.main-menu.workspaces li').droppable(options);
           }
          });
       }

      ...
      .
      .
})(jQuery);