0

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

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

3 回答 3

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");
});
于 2012-11-22T05:04:21.100 回答
0

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

document.cookie = cookieName + cookieContent + cookieExpires;

于 2012-11-20T15:39:04.083 回答
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;
}
于 2012-11-20T15:41:01.083 回答