2012-11-18 165 views
0

我搞砸了一些代碼,我有一個列表,我已經應用可拖動功能,另一個div與拖放功能。刪除列表項後刪除它

我想要實現的是當列表元素被刪除時,它將從父列表中刪除並添加到另一個列表中。

這裏是我與打碼:

<div id='dAvail'> 
<ul> 
    <li><div class='abox'>1</div></li> 
    <li><div class='abox'>2</div></li> 
</ul> 
</div> 
<div id='somewhereToDrop'> 
</div> 
<div id='newListHere'> 
<ul> 
</ul> 
</div> 

的JS:

$(document).ready(function(){ 
//add the droppable function to the divs in main list 
$('#dAvail ul).each(function(){ 
    $('li>div).draggable(); 
}); 

$('#somewhereToDrop').droppable({ 
    tolerance:'touch', 
    drop:function(event,ui){ 
    //this is where I am having problems. 
    //if I do ui.draggable I get an object representing the object I dragged but not 100% how to remove said object from the orginal list to then append to the new one. 
    //this is what i tried. 
    var t = ui.draggable[0]; //represents the div that i dropped. 
    $(t).parent().parent().remove($(t).parent()); 
    //that is as far as I have gotten as this returns an error 


}); 

如果有人能指出如何去從列表中刪除該對象將是巨大的。 我甚至試圖看看我是否可以做$(t).parent()。eq()但沒有用,這個想法是得到列表索引然後像這樣刪除它。

感謝

回答

1

remove()並不需要的參數。它從DOM中刪除它的實例。所以,真的,所有你想要做的是:

$(t).parent().remove(); 
+0

謝謝你。我想到,我實際上不想調用remove,只是將它附加到新列表中,但是這幫助我解決了如何到達那裏。 – Vade

+0

如果您不想完全刪除,請嘗試使用.detach()。它會返回對象,並將其保存在內存中,但將其從DOM中刪除。這可以讓你稍後重新插入一個你想要的。 –