如何更新span标签的内容?

时间:2013-03-24 14:25:54

标签: javascript html5 updates session-storage

我正在创建一个用户的网页,然后点击按钮选择一个项目。所选项目的详细信息将显示在保管箱中。目前,如果用户再次选择数量,我可以让它更新数量。我遇到的问题是,如果用户首次点击btnBuy1(详情显示在dropbox中),然后点击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
            }
        });

1 个答案:

答案 0 :(得分:0)

我不是一个jQuery人,但我认为你的问题是由于你增加了计数然后替换了Dropbox中的所有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'));