美好的一天,
我在提交表单时尝试删除cookie,但它无效。
这是我的代码:
$("form").submit(function()
{
var cookiePath = "Table_Rows-"+$(location).attr('pathname');
alert(cookiePath);
$.removeCookie(cookiePath);
}
即使我使用这些代码删除了所有cookie,它仍然无效:
$("form").submit(function()
{
for (var it in $.cookie()) $.removeCookie(it);
}
我通过onbeforeuload函数设置我的数据保存在cookie上
window.onbeforeunload = function(event)
{
storeRowData();
console.log("cookie saved!");
};
这是我的StoreData()js
var storeRowData = function () {
var data = [];
$('#tblItemList tbody>tr').each(function () {
var $this = $(this),
pId = $this.find("#itemId").val();
pname = $this.find('input.itemSearch').attr("value"),
desc = $this.find(".description").val(),
quant = $this.find(".qty").val(),
rowId = $this.find(".rowId").val(),
deleted = $this.find(".hidden-deleted-id").val(),
price = $this.find(".price").val();
var temp = {
productName: pname,
itemId:pId,
description: desc,
quantity: quant,
price: price,
deleted:deleted,
rowId: rowId };
data.push(temp);
});
var cookiePath = 'Table_Rows-'+$(location).attr('pathname');
$.cookie(cookiePath, JSON.stringify(data), {expires: 7});
}
然后即时检查是否在document.ready函数的页面上设置了cookie:
if($.cookie('Table_Rows-'+$(location).attr('pathname'))){
loadCookieData();
$(".qty, .price").bind("keyup change", calculate);
}
else{
addRow(0);
}
和我的loadCookieData()
var cookiePath = 'Table_Rows-'+$(location).attr('pathname');
//console.log("loadCookieData:"+cookiePath);
temp = $.cookie(cookiePath);
var parseData = JSON.parse(temp);
//console.log(parseData);
var html ='';
for (i in parseData) {
subTotal = parseData[i].quantity*parseData[i].price;
var st = new Number(subTotal);
var sub = st.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,');
html+= (some html code to replace here)
}
$("#tblItemList tbody").html(html);
for (i in parseData){
var inputBox = "#itemName"+parseData[i].rowId;
$(inputBox).select2(sOptions);
}
calculate();
}
我当前的代码有问题吗?
有人能告诉我如何正确地做到这一点吗?非常感谢你!
答案 0 :(得分:1)
改变了我的回答,这不再是一个限制性问题
document.cookie = cookiePath + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";