我用JS+Cookie编写的购物车,(添加、修改过程在客户端执行,一次提交服务器,IE适用)
二、js函数:orderform_functions.js
//Vampirebat,2002-1-16;http://www.vampirebat.org
//<--Start--从cookie中读出订单数据的函数
function ReadOrderForm(name)
{
var cookieString=document.cookie;
if (cookieString=="")
{
return false;
}
else
{
var firstChar,lastChar;
firstChar=cookieString.indexOf(name);
if(firstChar!=-1)
{
firstChar+=name.length+1;
lastChar = cookieString.indexOf(';', firstChar);
if(lastChar == -1) lastChar=cookieString.length;
return cookieString.substring(firstChar,lastChar);
}
else
{
return false;
}
}
}
//-->End
//<--Start--添加商品至购物车的函数
function SetOrderForm(item_no,item_name,item_amount,item_price)
{
var cookieString=document.cookie;
if (cookieString.length>=4000)
{
alert("您的订单已满\n请结束此次订单操作后添加新订单!");
}
else if(isNaN(item_amount)||item_amount<1||item_amount.indexOf('.')!=-1)
{
alert("数量输入错误!");
}
else
{
var mer_list=ReadOrderForm('24_OrderForm');
var Then = new Date();
Then.setTime(Then.getTime()+30*60*1000);
var item_detail="|"+item_no+"&"+item_name+"&"+item_amount+"&"+item_price;
if(mer_list==false)
{
document.cookie="24_OrderForm="+escape(item_detail)+";expires=" + Then.toGMTString();
alert("“"+item_name+"”\n"+"已经加入您的订单!");
}
else
{
if (mer_list.indexOf(item_no)!=-1)
{
alert('此商品您已添加\n请进入订单修改数量!')
}
else
{
document.cookie="24_OrderForm="+mer_list+escape(item_detail)+";expires=" + Then.toGMTString();
alert("“"+item_name+"”\n"+"已经加入您的订单!");
}
}
}
}
//-->End
//<--Start--订单修改更新cookie的函数
function updateCookie()
{
var limit=document.OrderForm.elements.length-3;
if (document.OrderForm.update.value=="修改订单")
{
document.OrderForm.update.value="更新订单";
document.OrderForm.submit.disabled=true;
//<--
for (i=1;i<limit;i++)
{
document.OrderForm.elements[i].disabled=false;
i=i+2;
document.OrderForm.elements[i].disabled=false;
}
//-->
}
else
{
document.OrderForm.update.value="修改订单"
document.OrderForm.submit.disabled=false;
//<--
for (i=1;i<limit;i++)
{
document.OrderForm.elements[i].disabled=true;
i=i+2;
document.OrderForm.elements[i].disabled=true;
}
//-->
var Then = new Date();
Then.setTime(Then.getTime()+30*60*1000);
var values=document.OrderForm.storage.value
document.cookie="24_OrderForm="+escape(values)+";expires=" + Then.toGMTString();
}
}
//-->End
//<--Start--删除单个商品的函数
function Delete(TableID,id)
{
var confirm_delete=window.confirm("确定要删除吗?")
if (confirm_delete)
{
var deletedList=document.OrderForm.storage.value;
var broken_deletedList=deletedList.split("|");
for (i=1;i<broken_deletedList.length;i++)
{
if(broken_deletedList[i].indexOf(id)!=-1) delete broken_deletedList[i];
}
var new_deletedList="";
for (i=1;i<broken_deletedList.length;i++)
{
if (broken_deletedList[i]!=undefined) new_deletedList=new_deletedList+"|"+broken_deletedList[i];
}
document.OrderForm.storage.value=new_deletedList;
TableID.deleteRow();
updateTotal();
}
}
//-->End
//<--Start--更新价格总计的函数
function updateTotal()
{
var new_total=0;
var limit=document.OrderForm.elements.length-3;
for (i=2;i<limit;i=i+3)
{
new_total=new_total*100+document.OrderForm.elements[i].value*100;
new_total=new_total/100;
}
if(isNaN(new_total))
{
document.OrderForm.total.value="输入错误";
}
else
{
document.OrderForm.total.value=new_total;
}
}
//-->End
//<--Start--更新价格小计的函数
function updateSubTotal(m_id,new_amount,price)
{
var price=price*100;
var new_subtotal=new_amount*price/100;
var n_m_id="id"+m_id;
if(isNaN(new_amount)||new_amount<1||new_amount.indexOf('.')!=-1)
{
document.OrderForm.elements[n_m_id].value="输入错误";
}
else
{
document.OrderForm.elements[n_m_id].value=new_subtotal;
}
updateTotal();
}
//-->End
//<--Start--更新storage的函数
function updateStorage(m_id,new_amount)
{
if(isNaN(new_amount)||new_amount<1||new_amount.indexOf('.')!=-1)
{
alert("数量填写错误!")
document.OrderForm.elements["aid"+m_id].focus();
document.OrderForm.elements["aid"+m_id].select();
}
else
{
var the_list=document.OrderForm.storage.value;
var single_list=the_list.split("|")
for (i=1;i<single_list.length;i++)
{
if (single_list[i].indexOf(m_id)!=-1)
{
var old_single=single_list[i];
var broken_single_list=single_list[i].split("&");
broken_single_list[2]=new_amount;
var new_single=broken_single_list[0]+"&"+broken_single_list[1]+"&"+broken_single_list[2]+"&"+broken_single_list[3];
the_list=the_list.replace(old_single,new_single);
}
}
document.OrderForm.storage.value=the_list;
}
}
//-->End
//Vampirebat,2002-1-16;http://www.vampirebat.org
//<--Start--从cookie中读出订单数据的函数
function ReadOrderForm(name)
{
var cookieString=document.cookie;
if (cookieString=="")
{
return false;
}
else
{
var firstChar,lastChar;
firstChar=cookieString.indexOf(name);
if(firstChar!=-1)
{
firstChar+=name.length+1;
lastChar = cookieString.indexOf(';', firstChar);
if(lastChar == -1) lastChar=cookieString.length;
return cookieString.substring(firstChar,lastChar);
}
else
{
return false;
}
}
}
//-->End
//<--Start--添加商品至购物车的函数
function SetOrderForm(item_no,item_name,item_amount,item_price)
{
var cookieString=document.cookie;
if (cookieString.length>=4000)
{
alert("您的订单已满\n请结束此次订单操作后添加新订单!");
}
else if(isNaN(item_amount)||item_amount<1||item_amount.indexOf('.')!=-1)
{
alert("数量输入错误!");
}
else
{
var mer_list=ReadOrderForm('24_OrderForm');
var Then = new Date();
Then.setTime(Then.getTime()+30*60*1000);
var item_detail="|"+item_no+"&"+item_name+"&"+item_amount+"&"+item_price;
if(mer_list==false)
{
document.cookie="24_OrderForm="+escape(item_detail)+";expires=" + Then.toGMTString();
alert("“"+item_name+"”\n"+"已经加入您的订单!");
}
else
{
if (mer_list.indexOf(item_no)!=-1)
{
alert('此商品您已添加\n请进入订单修改数量!')
}
else
{
document.cookie="24_OrderForm="+mer_list+escape(item_detail)+";expires=" + Then.toGMTString();
alert("“"+item_name+"”\n"+"已经加入您的订单!");
}
}
}
}
//-->End
//<--Start--订单修改更新cookie的函数
function updateCookie()
{
var limit=document.OrderForm.elements.length-3;
if (document.OrderForm.update.value=="修改订单")
{
document.OrderForm.update.value="更新订单";
document.OrderForm.submit.disabled=true;
//<--
for (i=1;i<limit;i++)
{
document.OrderForm.elements[i].disabled=false;
i=i+2;
document.OrderForm.elements[i].disabled=false;
}
//-->
}
else
{
document.OrderForm.update.value="修改订单"
document.OrderForm.submit.disabled=false;
//<--
for (i=1;i<limit;i++)
{
document.OrderForm.elements[i].disabled=true;
i=i+2;
document.OrderForm.elements[i].disabled=true;
}
//-->
var Then = new Date();
Then.setTime(Then.getTime()+30*60*1000);
var values=document.OrderForm.storage.value
document.cookie="24_OrderForm="+escape(values)+";expires=" + Then.toGMTString();
}
}
//-->End
//<--Start--删除单个商品的函数
function Delete(TableID,id)
{
var confirm_delete=window.confirm("确定要删除吗?")
if (confirm_delete)
{
var deletedList=document.OrderForm.storage.value;
var broken_deletedList=deletedList.split("|");
for (i=1;i<broken_deletedList.length;i++)
{
if(broken_deletedList[i].indexOf(id)!=-1) delete broken_deletedList[i];
}
var new_deletedList="";
for (i=1;i<broken_deletedList.length;i++)
{
if (broken_deletedList[i]!=undefined) new_deletedList=new_deletedList+"|"+broken_deletedList[i];
}
document.OrderForm.storage.value=new_deletedList;
TableID.deleteRow();
updateTotal();
}
}
//-->End
//<--Start--更新价格总计的函数
function updateTotal()
{
var new_total=0;
var limit=document.OrderForm.elements.length-3;
for (i=2;i<limit;i=i+3)
{
new_total=new_total*100+document.OrderForm.elements[i].value*100;
new_total=new_total/100;
}
if(isNaN(new_total))
{
document.OrderForm.total.value="输入错误";
}
else
{
document.OrderForm.total.value=new_total;
}
}
//-->End
//<--Start--更新价格小计的函数
function updateSubTotal(m_id,new_amount,price)
{
var price=price*100;
var new_subtotal=new_amount*price/100;
var n_m_id="id"+m_id;
if(isNaN(new_amount)||new_amount<1||new_amount.indexOf('.')!=-1)
{
document.OrderForm.elements[n_m_id].value="输入错误";
}
else
{
document.OrderForm.elements[n_m_id].value=new_subtotal;
}
updateTotal();
}
//-->End
//<--Start--更新storage的函数
function updateStorage(m_id,new_amount)
{
if(isNaN(new_amount)||new_amount<1||new_amount.indexOf('.')!=-1)
{
alert("数量填写错误!")
document.OrderForm.elements["aid"+m_id].focus();
document.OrderForm.elements["aid"+m_id].select();
}
else
{
var the_list=document.OrderForm.storage.value;
var single_list=the_list.split("|")
for (i=1;i<single_list.length;i++)
{
if (single_list[i].indexOf(m_id)!=-1)
{
var old_single=single_list[i];
var broken_single_list=single_list[i].split("&");
broken_single_list[2]=new_amount;
var new_single=broken_single_list[0]+"&"+broken_single_list[1]+"&"+broken_single_list[2]+"&"+broken_single_list[3];
the_list=the_list.replace(old_single,new_single);
}
}
document.OrderForm.storage.value=the_list;
}
}
//-->End