2012-02-17 28 views
0

我創建運行庫(爵士) 一些元素,使它們可拖動:重新在註冊jquery.Ui拖動

function fillAttachmentsPerPage(pageNum) { 
    createAjaxRequest("Manager/GetAttachments", { 
     'locationId': current_folder.attr('id'), 
    }).done(function (res) { 

     for (var i = 0; i < res.Attachments.length; i++) { 

      attachmentDiv = '<div class=\"attachmentDiv ui-draggable\" id="'+ res.Attachments[i].AttachmentId +'" onmouseover=\"attachmentDiv_hover(this);\"'+ 
      'onmouseout=\"attachmentDiv_onmouseout(this);\">' + 
'</div>​'; 

     } 



     $(".attachmentDiv").draggable({ 
     // start: function(event, ui) { ... } 
      revert: 'invalid', 
      drag: function(event, ui) { console.log("dragging") } 
     }); 


     $('a:has(ins)').droppable({ 
      drop: function(event, ui) { 
      console.log($(this).parent()) 
      setCurrentAttachment(ui.draggable.attr("id")); 
      UpdateAttachmentLocation(ui.draggable.attr("id"), $(this).parent().attr("id")) 

      } 
     }); 

    }); 
} 


$(".attachmentDiv").draggable({ 
// start: function(event, ui) { ... } 
    revert: 'invalid', 
    drag: function(event, ui) { console.log("dragging") } 
}); 

它的偉大工程。

後,我在dragable元素

上放棄這一元素我再次創建相同的元素,但沒有一個人掉隊。

上述註冊代碼再次執行,但元素不可拖動。

任何想法爲什麼?

+0

我們需要看到更多。它添加的新項目與該選擇器匹配,並且該代碼在創建後運行,然後它們將變爲可拖動狀態。所以這個故事還有更多。你能提供一個鏈接或設置一個小提琴嗎? – Sinetheta 2012-02-17 18:00:29

+0

我總是遇到困難託管我的js到jsfiddle。我可以在哪裏輕鬆地免費託管? – 2012-02-17 19:35:04

+0

增加了更多代碼 – 2012-02-17 19:38:02

回答

1

因爲元素一旦加載就被添加,您可能需要使用jQuery .on方法。但是,我不認爲.draggable方法適用於.on

解決此獲得的一種方式是使用jQuery下面的代碼來擴展:

(function($) { 
    $.fn.liveDraggable = function(opts) { 
     this.on("mousemove", function() { 
      $(this).draggable(opts); 
     }); 
    }; 
}(jQuery)); 

您需要添加它,你已經加載的jQuery後,但你嘗試在代碼中使用它。然後,而不是使用:

$(".attachmentDiv").draggable 

您使用:

$(".attachmentDiv").liveDraggable 
+0

不,.on()不是所有ajax的神奇解決方案。 – Sinetheta 2012-02-17 17:58:58