如何更新span标记的内容



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

我不是jQuery专家但我认为你的问题是由于你在增加计数然后替换dropbox中的所有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/>");

你不应该替换$("#0")而不是$("#dropbox")的内容吗?…例如

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

相关内容

  • 没有找到相关文章

最新更新