2012-12-18 48 views
1

我創建一個拖動德爾馬克拖車&。但我有這個JavaScript jQuery腳本一個問題,我花了一天找到這些代碼錯誤:請幫我看看我的jQuery代碼中的錯誤?

function addToCart(jQueryitem) { 
     var jQuerylist = jQuery("ul", jQueryhv_cart_items).length ? 
     jQuery("ul", jQueryhv_cart_items) : 
      jQuery("<ul class='gallery ui-helper-reset'/>").appendTo(jQueryhv_cart_items); 

     //ajax add to cart 
     var hvvid = jQueryitem.attr("id"); 
     var hvvcatid = jQueryitem.attr("catid"); 

     jQuery.get("index.php?quantity[]=1&addtocart=Thêm vào giỏ hàng&option=com_virtuemart&view=cart&task=add&virtuemart_product_id[]="+hvvid+"&virtuemart_category_id[]="+hvvcatid,function(data,status){ 
      //alert("Data: " + data + "\nStatus: " + status); 
     }); 

     //@hoangvi: find product has the same id with hvvid 
     //if founded update product quantity 
     //otherwise add product to cart 
     var total_li = jQuerylist.find("li").length; 
     for (var i=0; i<total_li; ++i) { 
      var current_li = jQuerylist.find("li")[i]; 
      //alert(jQuery('<div/>').append(current_li.clone()).html()); 
      var li_hidden = current_li.find("span.hv_hidden"); 
      //alert(jQuery('<div/>').append(li_hidden.clone()).html()); 
      var id = li_hidden.attr("id"); 
      //alert(id); 
      if (id == hvvid) { //founded 
       var quantity = li_hidden.attr('quantity'); 
       //alert(quantity); 
       quantity = quantity+1; 
       li_hidden.attr('quantity',quantity); 
       return; 
      } 
     } 

     //add product to cart 
     var jQueryitem2 = jQuery('<li class="ui-widget-content ui-corner-tr"></li>'); 
     var jQueryitem_img = jQuery("<div />").append(jQueryitem.find('img').clone()).html(); 

     jQueryitem2.append(jQueryitem_img).append('<span>SL: 1</span> ') 
     .append('<span class="hv_hidden" id="' + hvvid + '" quantity="1"> </span> '); 
     jQueryitem2.appendTo(jQuerylist).fadeIn(function() { 
      jQueryitem2 
       .animate({ width: "48px" }) 
       .find("img") 
        .animate({ height: "36px" }); 
     }); 
     jQueryitem2.append() 
    //}); 
} 

我覺得這個問題在這些代碼:

 //@hoangvi: find product has the same id with hvvid 
     //if founded update product quantity 
     //otherwise add product to cart 
     var total_li = jQuerylist.find("li").length; 
     for (var i=0; i<total_li; ++i) { 
      var current_li = jQuerylist.find("li")[i]; 
      //alert(jQuery('<div/>').append(current_li.clone()).html()); 
      var li_hidden = current_li.find("span.hv_hidden"); 
      //alert(jQuery('<div/>').append(li_hidden.clone()).html()); 
      var id = li_hidden.attr("id"); 
      //alert(id); 
      if (id == hvvid) { //founded 
       var quantity = li_hidden.attr('quantity'); 
       //alert(quantity); 
       quantity = quantity+1; 
       li_hidden.attr('quantity',quantity); 
       return; 
      } 
     } 

,因爲它正常工作時我刪除上面的代碼(但不能更新產品數量,當我添加兩個產品具有相同的ID)。我認爲這是一個語法錯誤,因爲它使jquery不能正常工作。 html代碼是這樣的:

<ul class="gallery ui-helper-reset"> 
           <li class="ui-widget-content ui-corner-tr" style="width: 48px;"><img src="/chovietnam.com/images/stories/virtuemart/product/resized/vantech-vt-3500i_90x90.jpg" alt="vantech-vt-3500i" class="featuredProductImage" border="0" style="height: 36px;"><span>SL: 1</span> <span class="hv_hidden" id="24" quantity="1"> </span> </li><li class="ui-widget-content ui-corner-tr" style="width: 48px;"><img src="/chovietnam.com/images/stories/virtuemart/product/resized/lenovo-g460small_90x90.jpg" alt="lenovo-g460small" class="featuredProductImage" border="0" style="height: 36px;"><span>SL: 1</span> <span class="hv_hidden" id="21" quantity="1"> </span> </li><li class="ui-widget-content ui-corner-tr" style="width: 48px;"><img src="/chovietnam.com/images/stories/virtuemart/product/resized/designer-handbag-223093101833428290_90x90.jpg" alt="designer-handbag-223093101833428290" class="featuredProductImage" border="0" style="height: 36px;"><span>SL: 1</span> <span class="hv_hidden" id="19" quantity="1"> </span> </li></ul> 

任何人都可以幫我解決這個問題,謝謝

+0

你得到什麼錯誤? –

+0

當我刪除「var total_li ...」和「... return;}}之間的代碼」「所有產品都可以拖放到購物車,但是當我插入這些代碼時,第一個產品購物車下降到購物車成功,第二個+不能下降到購物車,我認爲這意味着jQuery無法在達到「var total_li ...」和「... return;}}之間的代碼時工作」「 – user1912285

回答

1

的錯誤是在下面的線。

var current_li = jQuerylist.find("li")[i]' // This gets the DOM Object 

var li_hidden = current_li.find("span.hv_hidden"); 

current_liDOM對象,而不是一個jQuery對象

所以current_li應該$(current_li)

轉換到這一個jQuery對象使用.find()

+0

非常感謝您的幫助,我修復了這個錯誤(並且瞭解了更多內容) – user1912285

+0

很高興有幫助:) –

0

前...代碼(但當我添加兩類產品無法更新產品的數量有 相同的ID

你不能在頁面上的元素用相同的ID。 除非你正在過濾它。這裏有一個例子:

<div class="products"> 
    <span id="item1">My Item</span> 
</div> 
<div class="categories"> 
    <span id="item1">My Item 2</span>  
</div> 


$(function() { 
    //This will only find the first item with a duplicate ID 
    var $items = $("#item1"); 
    alert("Only the first item: " + $items.text()); 

    //This will find each duplicate id since it's setting the context either to ".products" or ".categories" 
    var $items = $("#item1", ".products"); 
    alert("Products Item: " + $items.text()); 

    var $items = $("#item1", ".categories"); 
    alert("Category Item: " + $items.text()); 
}); 

參見:http://jsfiddle.net/anAgent/eGSpw/

+0

我想你誤解了我,我說產品ID意思是「virtuemart-product-id」不是HTML元素ID,但謝謝你的幫助 – user1912285