在javascript中的单个cookie中检索多个cookie值

在javascript中的单个cookie中检索多个cookie值,javascript,Javascript,我以前发布过这个问题,处理购物车应用程序。到目前为止,该应用程序完成了我希望它完成的所有操作。我能够存储和检索饼干。但现在,我的问题是从cookies中提取单个值,并将它们放入我创建的表中。这是检索时cookie的格式。 ItemName=数量$价格。我试图做的是从cookie中检索数量和价格值,并将这些数据显示到我的表中。cookie将在“我的商店”页面中创建,然后在“我的购物车”页面中检索和显示。这是我的商店页面的代码 store.html <html> <head>

我以前发布过这个问题,处理购物车应用程序。到目前为止,该应用程序完成了我希望它完成的所有操作。我能够存储和检索饼干。但现在,我的问题是从cookies中提取单个值,并将它们放入我创建的表中。这是检索时cookie的格式。 ItemName=数量$价格。我试图做的是从cookie中检索数量和价格值,并将这些数据显示到我的表中。cookie将在“我的商店”页面中创建,然后在“我的购物车”页面中检索和显示。这是我的商店页面的代码

store.html

<html>
<head>
<title> Store </title>
<h1> My store </h1>
<script type="text/javascript">



function retr(circle)
{
var cke = document.cookie.split(';');
var nameCk= name + "=";
for(var i=0; i < cke.length; i++)
{
var c = cke [i];
while (c.charAt(0)==' ') c = c.substring(1, c.length);
if (c.indexOf(nameCk) == 0) 
return c.substring(nameCk.length, c.length);
}
return null;
}
function createCookie() 
{
var exdate = new Date();
exdate.setDate(exdate.getDate() +10);
if (document.getElementById("circle").checked)
{
document.cookie = "Circle=" + document.getElementById("quantity1").value + document.getElementById("price1").value + ";expires="+exdate.toGMTString();
}
}



function retrieve() 
{
if (document.getElementById("circle").checked)
{
document.getElementById("ret").value = document.cookie;
}
}

function Calc()
{
var fpri = document.getElementById("price1").value;
var pri = fpri.substr(1);
var qty = document.getElementById("quantity1").value;
var spri = document.getElementById("price2").value;
var pri2 = spri.substr(1);
var qty2 = document.getElementById("quantity2").value;

if (document.getElementById("circle").checked)
{
document.getElementById("total").value ='$' + Number(pri) * 
Number(qty);
}
else
document.getElementById("total").value = '$' + Number(pri2) * 
Number(qty2);
}
</script>

</head>

<body>
<table border = "1">
<tr>
<td> <input type="checkbox" id="circle" /> Circle </td>
<td> <img src="circle.jpg"> </img> </td>
<td> Price: <input type="text" size="4"   value = "$10.00" 
id="price1" name = "price1" /></td>
<td> Quantity: <input type="text" size = "4"  
id="quantity1"/></td>
</tr>
<tr>
<td> <input type = "checkbox" id="stickman" /> Stickman </td>
<td> <img src = "stickman.gif"> </img> </td>
<td> Price: <input type="text" size="4" value = "$5.00" 
id="price2" /> </td>
<td> Quantity: <input type="text" size="4"  id="quantity2" /> 
</td>
</tr>

</table>
<br />
<input type = "button" value = "Add to cart" 
onclick="createCookie()">
<br />
<br />
<a href ="cart.html" > View Cart </a>
<br /> 
<input type = "text" size = "8" id = "total" readonly = "readonly" 
/> Total
<br /> 
<input type = "button" id = "calcu" value = "calc" onclick = "Calc
()" />
<input type = "button" value = "retrieve" onclick = "retrieve()" />
<input type = "text" readonly = "readonly"  name = "ret" id = 
"ret"/>
</body>
</html>

商场
我的商店
函数retr(圆)
{
var cke=document.cookie.split(“;”);
变量nameCk=name+“=”;
对于(变量i=0;i



全部的
“计算”和“检索”按钮以及“只读”文本框都在那里,这样我就知道cookie正在被存储和检索,并且我能够得到总数,它将显示在我的购物车页面上

这是我的购物车页面的代码 cart.html

<html>
<head>
<title> Cart </title>
<h1> My cart </h1>
<script type = "text/javascript">

function retr(circle)
{
document.getElementById("q2").value = document.getElementById("quantity1").value

}

function retrieve() 
{
var quvalue = retr("circle")
if (quvalue != false) {
document.getElementById("q2").value = quvalue
}
document.getElementById("ret").value = document.cookie;

}


</script>
</head>

<body onload = "retrieve()">
<table border = "1">
<td>Stickman </td>
<td><input type = "text" size = "8" id = "q2" readonly = "readonly" /></td> 
<td id ="p1"> price per </td>
<td id ="t1"> total </td>
<tr> </tr>
<td> Circle </td>
<td> quantity order </td>
<td> price per </td>
<td> total </td>
<tr> </tr>
<td colspan = "3"> TOTAL: </td>
<td> total price </td>
</table> 
<br /> <br />
<input type ="text" id = "ret" readonly = "readonly" />
<br / > <br />
<input type = "button" value = "Checkout">
<br /> <br />
<a href = "store.html" > Continue Shopping </a>


</body>
</html>

运货马车
我的手推车
函数retr(圆)
{
document.getElementById(“q2”).value=document.getElementById(“quantity1”).value
}
函数检索()
{
var quvalue=retr(“圆”)
如果(quvalue!=false){
document.getElementById(“q2”).value=quvalue
}
document.getElementById(“ret”).value=document.cookie;
}
斯蒂克曼
价格
全部的
圆圈
数量订单
价格
全部的
总数:
总价






应该在表中检索的值是使用onload事件属性调用的,该属性应该调用retrieve()函数


另外,我知道我在carts页面中创建表的方式并不完全正确。但我现在主要关注的是获取表中显示的必要值,然后我将返回重写表代码。

这有很多错误。。。对您的cookie代码进行简短的检查似乎可以,但有点过于复杂。在把你的值塞进cookies之前,先研究JSON来解析和字符串化你的值——它会改变你的生活。您正在本地测试此文件吗?如果没有通过某种web服务器运行,则无法存储cookie。我正在本地进行测试。我知道还有很多其他的方法可以编写代码,但不幸的是,我只能使用我们在课堂上学到的东西。我认为我们还没有学会。我们也还没有使用表单。如果你没有通过网络服务器运行,那么cookies是不可能的。它们是来自web服务器的请求和响应的一部分,不能与本地文件相关存储。使用本地文件进行此操作的唯一方法是将变量传递到下一页的URL(store.html?name=value&name2=value2)中,并使用javascript从URL解析它们。