2013-03-24 184 views
0

我正在創建一個網頁,用戶通過點擊按鈕選擇一個項目。所選項目的詳細信息將顯示在保管箱中。目前,如果用戶再次選擇數量,我可以更新數量。我遇到的問題是,如果用戶首先點擊btnBuy1(細節顯示在保存箱中),然後點擊btnBuy2(同樣會顯示細節),但是當他們再次點擊btnBuy2時,會更新btnBuy2的詳細信息,但是來自btnBuy1的詳細信息消失。如何更新span標籤的內容?

 $("#btnBuy0").click(function() 
     { 
      if (!sessionStorage['quantity0']) 
      { 
       sessionStorage['quantity0'] = 1; 
       $("#dropbox").append('<span id = "0"><img class = "thumb" src="../images/21_metoyou.jpg" />' + teddy[0].desc + ", Price £" 
      + teddy[0].price + ", Quantity: " + sessionStorage.getItem('quantity0') + "</span><br/>"); 

      }   
      else 
      { 
       sessionStorage['quantity0']++; 
       $("#dropbox").html('<span id = "0"><img class = "thumb" src="../images/21_metoyou.jpg" />' + teddy[0].desc + ", Price £" 
      + teddy[0].price + ", Quantity: " + sessionStorage.getItem('quantity0') + "</span><br/>"); 

      } 
      if (Modernizr.sessionstorage) 
      { // check if the browser supports sessionStorage 
       myids.push(teddy[0].partnum); // add the current username to the myids array 
       sessionStorage["ids"]=JSON.stringify(myids); // convert it to a string and put into sessionStorage 
      } 
      else 
      { 
      // use cookies instead of sessionStorage 
      } 
     }); 
     $("#btnBuy1").click(function() 
     { 
      if (!sessionStorage['quantity1']) 
      { 
       sessionStorage['quantity1']=1; 
       $("#dropbox").append('<span id = "1"><img class = "thumb" src="../images/birthday_metoyou.jpg" />' + teddy[1].desc + ", Price £" 
      + teddy[1].price + ", Quantity: " + sessionStorage.getItem('quantity1') + "</span><br/>"); 

      } 
      else 
      { 
       sessionStorage['quantity1']++; 
       $("#dropbox").html('<span id = "1"><img class = "thumb" src="../images/birthday_metoyou.jpg" />' + teddy[1].desc + ", Price £" 
      + teddy[1].price + ", Quantity: " + sessionStorage.getItem('quantity1') + "</span><br/>"); 

      } 
      if (Modernizr.sessionstorage) 
      { // check if the browser supports sessionStorage 
       myids.push(teddy[1].partnum); // add the current username to the myids array 
       sessionStorage["ids"]=JSON.stringify(myids); // convert it to a string and put into sessionStorage 
      } 
      else 
      { 
      // use cookies instead of sessionStorage 
      } 
     }); 

回答

0

我不是一個jQuery的人,但我覺得你的問題是,你遞增計數,然後替換保管箱所有HTML造成的事實 - 可見

sessionStorage['quantity0']++; 
    $("#dropbox").html('<span id = "0"><img class = "thumb" src="..jpg" />' + 
// there -------^^^^----- 
         teddy[0].desc + ", Price £" 
        + teddy[0].price + ", Quantity: " + 
         sessionStorage.getItem('quantity0') + "</span><br/>"); 

不該你是不是替換$(「#0」)而不是$(「#dropbox」)的內容? ...即

sessionStorage['quantity0']++; 
    $("#0").html('<img class = "thumb" src="..jpg" />' + teddy[0].desc + 
        ", Price £" + teddy[0].price + ", Quantity: " + 
        sessionStorage.getItem('quantity0'));