2009-08-19 82 views
1

我正在使用jQuery和FoxyCart拖放購物車,拖放工作,但我不知道如何自動打開鏈接(在拖動元素內部)一旦項目被放入購物車。jQuery Drag&Drop自動打開鏈接裏面拖動元素

基本上我有一個清單<ul><li class="drag">和裏面我有<a href="..."><img src=".." alt="thumbnail"/></a><a href="..." class="addtocart">Add to cart</a>。添加到購物車的鏈接是使用css隱藏的,但我需要它,以便當我在購物車中放置物品時,它會打開這個隱藏的鏈接(因爲鏈接打開了一個模式框並將該物品添加到真正的購物車中由FoxyCart提供)。對我來說問題是我不知道jquery那麼好,我不知道如何過濾鏈接,然後打開它。

+0

你使用的是jQuery UI的可拖拽/拖拽嗎? – 2009-08-19 02:18:53

+0

沒有人可以回答? ..至少告訴我它是否可能,因爲我瘋了試圖瞭解jQuery的文檔,我真的不知道我需要尋找的地方和東西:) – Raffaele 2009-08-20 13:01:21

+0

男人,看看蜘蛛網這裏。 – Will 2012-03-14 14:13:02

回答

5

如果列表看起來是這樣的:

<ul> 
    <li class="drag"> 
     <a href="whatever" class="link-to-open">this link will be opened when dropped</a> 
    </li> 
    <li class="drag"> 
     <a href="whatever" class="link-to-open">this link will be opened when dropped</a> 
    </li> 
</ul> 

在您的購物車投擲的對象:

$('#shopping-cart').droppable({ 
    drop: function(e,ui) { 
     $(ui.draggable).find('.link-to-open').click() 
    } 
}); 
+0

'$(ui.draggable).find('。link-to-open')。click()' – 2012-03-16 08:12:15

+0

我想,$(ui.draggable).find('。link-to-open')。trigger 「點擊」),而不是點擊(),是更多的地方(似乎更多的權利;)) – NickGreen 2012-03-20 14:21:38

+0

我原本有'trigger('click')',我通常使用它,但爲了簡單起見,我改變了它點擊。 – nathanjosiah 2012-03-20 15:21:23

4

這裏是你的代碼:

$('.drag').draggable({ 
       revert: "invalid", 
       helper: "clone" 
      }); 
$('.cart').droppable({ 
    accept: ".drag", 
    drop: function(event, ui){  
     window.location=$(ui.draggable).find("a.addtocart").attr("href"); 
    } 
}); 

這裏是DEMO

1

關於您要拖動的元素,如具有縮略圖和項目名稱的DIV等,您需要爲其提供類似shoppingcartItem(或您所選擇的其他任何內容)

該項目的jQuery您拖動,然後將

$('.shoppingcartItem').draggable({ 
    /* Don't forget, you can change this class name to match what you have */ 
    revert: "invalid", 
    helper: "clone" 
}); 

然後,你需要有你的項拖放到伴隨着「購物車」的行類或ID你車區域,我認爲ID更有意義在這種情況下,你只能有一個購物車,而這個購物車只能用作購物車。

$('#shoppingCart').droppable({ 
    accept: ".shoppingcartItem", /* this needs to match up with what you used */ 
    drop: function(event, ui){  
     window.location=$(ui.draggable).find("a.postAddToCart").attr("href"); 
    } 
}); 

現在,解釋「功能」在這裏,這是搜索鏈接標籤,其中有一類postAddToCart,然後調用鏈接標記的一個<a>標籤。雖然這會在同一窗口中打開鏈接。我不太確定這是你想要的,如果不是這樣的話,this可以幫助你出來