如何创建一个cookie按钮,跨页面存储多个值

我每次点击相同的按钮时都使用以下代码设置一个cookie,其数值增加 每次单击它并将产品的标题保存为值(例如Name:1,Value:Car):</ p>

  var num = 1; 
function addCookie (){

var expString =“; expires =”+ date.toGMTString();
var cookievalue = escape(document.querySelector(“[name = addpart]”)。value)+“;”; \ n document.cookie = num +“=”+ cookievalue + expString +“; path = /”;

num ++;
}
</ code> </ pre>

它在我的页面上运行良好,每次点击为“1”,然后“2”时设置一个新的cookie名称 ,然后'3'等等,但显然只留下我:</ p>


  • 姓名:1价值:汽车</ li>
  • 姓名: 2值:汽车</ li>
  • 名称:3值:汽车</ li>
    </ ul>

    然而,当我转到另一页并再次单击该按钮时, 它会用名称1覆盖cookie并再次启动该过程。</ p>

    我想知道如何跨页面继续使用此按钮的名称计数?</ strong> 这样用户就可以浏览产品并单击按钮,该按钮基本上生成一个可以在以后调用的列表:</ p>


    • 名称:1值:Car </ li> \ n
    • 名称:2值:自行车</ li>
    • 名称:3值:训练</ li>
      </ ul>

      我知道创建起来会更容易 每个页面的新按钮,但我在我的wesbite上使用模板系统,我们有产品页面的thousdands,所以这对我来说不是一个可行的选择。</ p>
      </ DIV>

展开原文

原文

I am using the following code to set a cookie each time the same button is clicked, which has an increased numberical value each time it is clicked and saves the title of the product as the value (for example Name: 1, Value: Car):

var num=1;
  function addCookie() {

        var expString = "; expires=" + date.toGMTString();
        var cookievalue=escape(document.querySelector("[name=addpart]").value) + ";";
        document.cookie=num + "=" + cookievalue + expString + "; path=/"; 
        num++;
    }

It works nicely on my page, setting a new cookie name each time it is clicked as '1', then '2', then '3' and so on, but obviously that just leaves me with:

  • Name: 1 Value: Car
  • Name: 2 Value: Car
  • Name: 3 Value: Car

However, when I go to another page and click the button again, it overwrites the cookie with the name 1 and starts the process again.

I would like to know how I can continue the name count for this button across pages? So that the user can browse the products and click the button which essentially produces a list that can be called later:

  • Name: 1 Value: Car
  • Name: 2 Value: Bike
  • Name: 3 Value: Train

I understand it would be easier to create new buttons for each page, but I use a template system on my wesbite and we have thousdands of product pages so that's not a viable option for me.

doutao6653
doutao6653 “浏览产品并点击按钮,它实际上会生成一个可以在以后调用的列表”......那么购物车呢?他们通常会交给会话和数据库。
接近 2 年之前 回复
duanfen9983
duanfen9983 我认为更好的方法是在你的cookie中保存一个json编码的数组。有了这个,你不会在访问者的浏览器上创建很多cookie。对你来说,管理它更简单。
接近 2 年之前 回复

1个回答



在我看来使用localStorage而不是cookie时,很容易存储一个对象数组。</ p>

  //第一个结构的例子
localStorage.setItem(“visits”,JSON.stringify({“1”:[],“2”:[],“3”:[]}));

//获取项目
var visits = JSON.parse(localStorage.getItem(“visits”));

//不同的访问次数

访问[“1”]。推(“Car” );
visits [ “1”]推( “自行车”);。。
visits [ “1”]推( “东西”);

visits [ “2”]推( “自行车”); \ 。nvisits [ “2”]推( “自行车”);
visits [ “2”]推( “东西”);

visits [ “3”]推( “东西”);
visits [” 3“]。推(”Something“);
访问[”3“]。推(”Something“);

//设置项目
localStorage.setItem(”visits“,JSON.stringify(visits) );

console.log(访问)
</ code> </ pre>

此处示例 https://jsfiddle.net/0kp9s2bv/ </ p>
</ div>

展开原文

原文

Is pretty easy storing an array of objects, is better in my opinion using localStorage instead of cookie.

//example of first structure
localStorage.setItem("visits", JSON.stringify({"1":[],"2":[],"3":[]}));

//getting the item
var visits = JSON.parse(localStorage.getItem("visits"));

//different visits

visits["1"].push("Car");
visits["1"].push("Bike");
visits["1"].push("Something");

visits["2"].push("Bike");
visits["2"].push("Bike");
visits["2"].push("Something");

visits["3"].push("Something");
visits["3"].push("Something");
visits["3"].push("Something");

//setting the item
localStorage.setItem("visits", JSON.stringify(visits));

console.log(visits)

Example here https://jsfiddle.net/0kp9s2bv/

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐