Javascript 如何保存变量中的值?

Javascript 如何保存变量中的值?,javascript,php,arrays,json,console,Javascript,Php,Arrays,Json,Console,现在,我正在将变量中的值打印到控制台,但我真正想做的是,每次用户单击“.one”时,以某种方式将这些值保存到一个新数组中。这可能吗 这是我的密码: function addNewTimer() { //add a new timer $(".one .add").click(function() { $('.one').append('<span class="newtimer stoptimer">1000</span><br/>').htm

现在,我正在将变量中的值打印到控制台,但我真正想做的是,每次用户单击“.one”时,以某种方式将这些值保存到一个新数组中。这可能吗

这是我的密码:

function addNewTimer() {
  //add a new timer
  $(".one .add").click(function() {
    $('.one').append('<span class="newtimer stoptimer">1000</span><br/>').html();
    $('.one .newtimer:last').prop('Counter', 0).animate({
      Counter: 1000
    }, {
      duration: timerclock, //default 100 mins = 60000000,
      easing: 'linear',
      step: function(now) {
        $(this).text(Math.ceil(now));
      }

    });
    arr1 = {}
    arr1.role = $(".one .positiontitle").text();
    arr1.team = "Account";
    arr1.rate = cost1;
    arr1.time_start = new Date();
    arr1.time_start_perc = Number($(".stoptimer:first").text() / 60);
    console.log(arr1);
    $(".one").append('<span class="role"></span><br/>').html();
    $(".one").append('<span class="team"></span><br/>').html();
    $(".one").append('<span class="rate"></span><br/>').html();
    $(".one").append('<span class="time_start"></span><br/>').html();
    $(".one").append('<span class="time_start_perc"></span><br/>').html();
    $(".one .role:last").append(arr1.role);
    $(".one .team:last").append(arr1.team);
    $(".one .rate:last").append(arr1.rate);
    $(".one .time_start:last").append(arr1.time_start);
    $(".one .time_start_perc:last").append(arr1.time_start_perc);
  });
}
函数addNewTimer(){
//添加一个新计时器
$(“.one.add”)。单击(函数(){
$('.one').append('1000
').html(); $('.one.newtimer:last').prop('Counter',0)。设置动画({ 柜台:1000 }, { 持续时间:timerclock,//默认100分钟=60000000, “线性”, 步骤:功能(现在){ $(this.text(Math.ceil(now)); } }); arr1={} arr1.role=$(.one.positiontitle”).text(); arr1.team=“Account”; arr1.rate=cost1; arr1.time_start=新日期(); arr1.time_start_perc=数字($(“.stoptimer:first”).text()/60); 控制台日志(arr1); $(“.one”).append(“
”).html(); $(“.one”).append(“
”).html(); $(“.one”).append(“
”).html(); $(“.one”).append(“
”).html(); $(“.one”).append(“
”).html(); $(.one.role:last”).append(arr1.role); $(.one.team:last”).append(arr1.team); $(“.one.rate:last”).append(arr1.rate); $(“.one.time\u start:last”).append(arr1.time\u start); $(“.one.time\u start\u perc:last”).append(arr1.time\u start\u perc); }); }
您有以下选项:

  • 使用localStorage在浏览器中本地保存数据。这是一个非常简单的步骤,但是用户可以删除这些数据。如果要使用localStorage,请查看此链接:

  • 将数据保存在服务器上。因此,您需要一个后端来将数据保存到数据库、文件或其他任何地方。您可以编写一个非常简单的PHP/NodeJS后端,其中一个API端点用于保存,另一个用于加载数据。 您可以使用从服务器保存和加载数据


您可以发送ajax请求来存储数据

近控制台日志(arr1);使用以下命令(我想您使用的是jquery):

现在,您的store.php可能如下所示:

<?php

  $data = $_POST;

  var_dump($data); //debug if you want

  //Now you can store in your database.

请发布您的html,以便我们可以创建一个工作示例。刷新页面时,您的数组是否应该保持不变?您只需要在js中使用该数组还是在php中使用该数组(因为您添加了php标记),您只是想向数组添加一个值?使用
var myArray=[]在某处初始化数组
,然后添加
myArray.push(arr1)谢谢回复!问题是-我不想一直把这些值推到同一个数组中。我想在每次单击“.one”时创建一个全新的阵列。基本上,每次用户按下按钮时,我都想保存精确时刻的值,但我不知道该怎么做。你想如何保存这些值-永久(下次他访问网站时)还是临时(做一些有价值的事情)?-我的问题是,我不知道在单击之后,您希望如何处理您的值。如果你能更准确地解释你想做什么,我想我能帮你更多。好吧,我基本上希望我的值永久保存在一个单独的页面上。最好的方法是使用服务器后端和ajax调用,否则你可以使用浏览器的本地存储。
<?php

  $data = $_POST;

  var_dump($data); //debug if you want

  //Now you can store in your database.