2012-02-25 80 views
1

我正試圖將物品拖到購物籃中。一旦物品掉入籃筐,物品價格將更新爲籃筐的總價格。我錯過了什麼嗎?它允許我放入「購物車」,但不會更新總價。如何更新物品掉落時的總價格?

$(".item").draggable({ 
    revert : true 
}); 
$("#i1").draggable(); 
$("#i2").draggable(); 
$("#i3").draggable(); 
$("#cart").droppable({ 
    drop : function (event, ui) { 
     $(this).find(".price"); 
     total_price = total_price + price; 
     $("#cprice").html("$ " + total_price); 
    } 
}); 
+0

我想我們會需要html或其他東西... – elclanrs 2012-02-25 05:09:38

回答

0

哪裏total_priceprice定義的變量?

試試這個。

var total_price = 0;//Declare and initialize the variable to 0 

$("#cart").droppable({ 
    drop: function(event, ui) { 
    //Add the dragged item's price to total_price and update the cart price 
    total_price = total_price + parseFloat($(this).find(".price").text()); 
    $("#cprice").html("$ " + total_price); 
} 
+0

謝謝你的回答!我在我的拖放功能中替換了該行,並更新了總價格。不幸的是,總價格顯示爲「NaN」。那是什麼意思? – user1232103 2012-02-25 05:22:37

+0

這意味着'不是數字'。可以檢查我編輯的答案,我希望現在已經修復。 – ShankarSangoli 2012-02-25 05:24:26

+0

它正在返回「NaN」,因爲「.price」中的第一個數字是「$」。還有什麼我可以用來閱讀「。價格」? – user1232103 2012-02-25 05:34:59

相關問題