0

ユーザーがボタンをクリックしてアイテムを選択するWebページを作成しています。選択したアイテムの詳細がドロップボックスに表示されます。現時点では、ユーザーが再度選択した場合に数量を更新することができます。私が抱えている問題は、ユーザーが最初に btnBuy1 (ドロップボックスに表示される詳細) をクリックし、次に btnBuy2 をクリックすると (再び詳細が表示されます)、btnBuy2 をもう一度クリックすると、btnBuy2 の詳細が更新されますが、 btnBuy1 からの詳細は消えます。

        $("#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
            }
        });
4

1 に答える 1

0

私はjQueryの人ではありませんが、あなたの問題は、カウントを増やしてからドロップボックス内のすべてのHTMLを置き換えているという事実によって引き起こされていると思います-vis

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/>");

$("#dropbox") ではなく $("#0") の内容を置き換えるべきではありませんか?? ...つまり

sessionStorage['quantity0']++;
  $("#0").html('<img class = "thumb" src="..jpg" />' + teddy[0].desc + 
                     ", Price £" + teddy[0].price + ", Quantity: " + 
                     sessionStorage.getItem('quantity0'));
于 2013-03-24T14:33:45.300 に答える