简体   繁体   English

单击按钮时的Cookie值总和

[英]Sum Cookie value when I click a button

I have an input text and a button where a I put a number and storage this value in a Cookie using js-cookie clicking a button. 我有一个输入文本和一个按钮,在其中我输入了一个数字,然后使用js-cookie单击此按钮将该值存储在Cookie中。

<input id="number" type="text" value="0">
<button id="send" type="button">SEND</button>

I want to sum each time I enter a number and show the result in an alert, so I tried in this way: 我想在每次输入数字时求和,并在警报中显示结果,所以我尝试了这种方式:

Jquery: jQuery:

$("#number").keypress(function(e){
  if(e.keyCode==13){
    $("#send").click();
  }
});

$("#send").click(function(){ 
   var number= 0;
   number+= parseInt($("#number").val(),10);
   Cookies.set("numw", number);
});

var numw = Cookies.get("numw");
alert("Total: "+numw);

But this still showing the first number I sent. 但这仍然显示我发送的第一个号码。

I would like some help. 我需要一些帮助。

All you need is to add the number like: 您所需要做的就是添加数字,例如:

$("#send").click(function(){ 
    //var number= 0;
    var numwTemp = Cookies.get("numw");
    //number = parseInt($("#number").val(),10)) + parseInt(numwTemp, 10);
    var number = parseInt($("#number").val(),10)) + parseInt(numwTemp || "0", 10);
    Cookies.set("numw", number);
});

var numw = Cookies.get("numw");
alert("Total: "+numw);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM