将cookie值增加1

时间:2012-11-20 15:34:40

标签: cookies setcookie

我有一个文本字段和一个提交按钮。用户提交电子邮件ID,成功提交后将加载灯箱。成功提交后,我正在设置一个cookie并最初分配值1.尽管我能够设置增量,但我无法将增加的值分配给cookie值。

var demoCookieCount = getCookie("democount");
$('#online-demo-lightbox-button').click(function (e) {
e.preventDefault();
demoCookiecount++;
});

3 个答案:

答案 0 :(得分:0)

您不会将新值保存到Cookie中。最后尝试这样的事情:

document.cookie = cookieName + cookieContent + cookieExpires;

答案 1 :(得分:0)

您必须使用setCookie()或类似

之类的方法将新值存储在cookie中
function setCookie(democount, ++demoCookiecount, 1)
{
  var exdate=new Date();
  exdate.setDate(exdate.getDate() + exdays);
  var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
  document.cookie=c_name + "=" + c_value;
}

答案 2 :(得分:0)

终于以某种方式完成了。这段代码工作

//setting the Cookie democount with initial value of 1
setCookie("democount", '1');
//getting the value of 'democount' cookie and assigning it to a variable
var demoCookieCount = getCookie("democount");
$('#sample-button').click(function (e) {
e.preventDefault();
//increment the variable on every click
demoCookieCount++;
//set the incremented value of demoCookieCount variable to democount cookie
setCookie("democount",demoCookieCount);
// assigning the value of demoCookiecount to variable 123
var123 = getCookie("democount");
});