这段JS研究好长时间了,改不了,谁能
本身这段代码没问题.想加个功能这是个COOKIE ,我想让这段代码 实现这样功能
给这个代码一个ID值,让他运行的时候根据这个ID 重新建立cookie和读取cookie
大概意思就是,每个页面一个ID,每个页面都会记录不同的cookie 来读取,,,
这个ID就命名为pid吧,改了好多次,都出问题
代码
-----------------------------------------------------------------
<script>
<!--
function addcart(id,name,num,price){
var str="";
str=get_cookie("shoppingcart");
if(str.length>0){
arr=str.split("^");
count=arr.length;
comp="id,"+id;
newstr="";
flag=0;
for(i=0;i<arr.length-1;i++){
pro=arr[i].split("|");
if(comp==pro[0]){
flag=1;
pro_num=pro[2].split(",");
pro_num[1]=parseInt(pro_num[1])+1;
pro[2]=pro_num[0]+","+pro_num[1];
arr[i]=pro[0]+"|"+pro[1]+"|"+pro[2]+"|"+pro[3];
}
}
if(flag==0){
newstr="id,"+id+"|name,"+name+"|num,"+num+"|price,"+price+"^";
}
returnstr="";
for(i=0;i<arr.length-1;i++){
returnstr=returnstr+arr[i]+"^";
}
returnstr=returnstr+newstr+";";
add_cookie(returnstr);
}
else{
add_cookie("id,"+id+"|name,"+name+"|num,"+num+"|price,"+price+"^;");
}
}
function add_cookie(str){//写入cookie
document.cookie="shoppingcart="+str;
location.href="1.asp";
}
function read_cookie(){//读取购物车数据
str=get_cookie("shoppingcart");
arr=str.split("^");
all_money=0;
for(i=0;i<arr.length-1;i++){
pro=arr[i].split("|");
id_temp=pro[0].split(",");
id=id_temp[1];
name_temp=pro[1].split(",");
name=name_temp[1];
num_temp=pro[2].split(",");
num=num_temp[1];
price_temp=pro[3].split(",");
price=price_temp[1];
money=parseInt(num*price);
all_money=all_money+money;
document.write("<table width='90%' >");
document.write("<tr>");
document.write("<td width='50%' height='10' align='left'>");
document.write("<left>"+name+"");
document.write("</td>");
document.write("<td width='20%'align='right'>");
document.write("<right>"+num+"");
document.write("份");
document.write("<td width='30%' align='right'>");
document.write("×"+price+"");
document.write(".0元 ");
document.write(" <a href='###' onclick='javascript:delete_cookie("+id+");aaa.window.location.reload()'>Θ</a>");
document.write("</td>");
document.write("</right>");
document.write("</td>");
document.write("</tr>");
document.write("</table>");
}
document.write("<br>");
document.write("<right>总金额:"+all_money+"元</right>");
}
function delete_cookie(id){//删除某个产品
str=get_cookie("shoppingcart");
arr=str.split("^");
count=arr.length;
comp="id,"+id;
returnstr="";
for(i=0;i<arr.length-1;i++){
pro=arr[i].split("|");
if(comp!=pro[0]){
returnstr=returnstr+arr[i]+"^";
}
}
add_cookie(returnstr);
window.location.reload();
//alert(str);
}
function change_cookie(id,num){//更改数量
//alert(num);
num=parseInt(num);
//alert(num);
if(num==0){
alert("数量不能小于等于0");
location.href="1.asp"
}
else{
//alert("数量大于等于0了");
var str="";
str=get_cookie("shoppingcart");
if(str.length>0){
arr=str.split("^");
count=arr.length;
comp="id,"+id;
newstr="";
flag=0;
for(i=0;i<arr.length-1;i++){
pro=arr[i].split("|");
if(comp==pro[0]){
flag=1;
pro_num=pro[2].split(",");
pro_num[1]=num;
pro[2]=pro_num[0]+","+pro_num[1];
arr[i]=pro[0]+"|"+pro[1]+"|"+pro[2]+"|"+pro[3];
}
}
if(flag==0){
newstr="id,"+id+"|name,"+name+"|num,"+num+"|price,"+price+"^";
}
returnstr="";
for(i=0;i<arr.length-1;i++){
returnstr=returnstr+arr[i]+"^";
}
returnstr=returnstr+newstr+";";
add_cookie(returnstr);
}
else{
add_cookie("id,"+id+"|name,"+name+"|num,"+num+"|price,"+price+"^;");
}
}
}
function get_cookie(Name) {
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
if (offset != -1) {
offset += search.length
end = document.cookie.indexOf(";", offset);
if (end == -1)
end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}
-->
</script>
<script>
<!--
read_cookie();
-->
</script>
[ 本帖最后由 HOHOMEIMIA11 于 2012-7-19 15:57 编辑 ]