Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 存储总数,并在提交时累积_Javascript_Php_Angularjs_Local Storage_Cumulative Sum - Fatal编程技术网

Javascript 存储总数,并在提交时累积

Javascript 存储总数,并在提交时累积,javascript,php,angularjs,local-storage,cumulative-sum,Javascript,Php,Angularjs,Local Storage,Cumulative Sum,如何存储总数,并在用户每次点击提交时将其累积 例如,Cumulative Total=#将用户每次提交信息时第3列的总数相加 <body ng-app="Test"> <section style="margin-top:80px"> <h3>Plastic Calculator Form</h3> <div ng-controller="TestController as test" > <p

如何存储总数,并在用户每次点击提交时将其累积

例如,
Cumulative Total=#
将用户每次提交信息时第3列的总数相加

<body ng-app="Test">
  <section style="margin-top:80px">

    <h3>Plastic Calculator Form</h3>

    <div ng-controller="TestController as test" >
      <p>To date, <strong><u># of people who pledged</u></strong> Earthlings have pledged to reduce their single-use plastic waste from <strong><u>{{ test.approve | sumByColumn: 'amount' }}</u></strong> Items per year to <strong><u>{{(test.approve | sumByColumn: 'amount') - (test.approve | sumByColumn4: 'reducedTotal')}}</u></strong>. That's a reduction of <strong><u>{{ test.approve | sumByColumn4: 'reducedTotal' }}</u></strong> per year!  Do your part.  Make a pledge!</p>
      <table class="table">
        <tr>
          <th>Single-Use Plastic Items</th>
          <th>Enter the Number You Use Per Week</th>
          <th>The Number You Use Per Year is:</th>
          <th>How Many Less Can You Use Per Week?</th>
          <th>Your Reduced Usage Per Year Would Be:</th>
        </tr>

        <tr ng-repeat="x in test.approve">
          <td> {{ x.name }} </td>
          <td> <input class="qty form-control" type="number" ng-model="x.number" ng-change="sumByColumn3()" min="0" restrict-to="[0-9]"/> </td>
          <td> {{ x.number*x.amount }} </td>
          <td> <input class="qty form-control" type="number" ng-model="x.reducedAmount" ng-change="sumByColumn2()" min="0" restrict-to="[0-9]"/> </td>
          <td> {{ x.reducedAmount*x.reducedTotal }} </td>
        </tr>
        <tr>
          <td>TOTALS</td>
          <td>{{ test.approve | sumByColumn3: 'number' }}</td>
          <td>{{ test.approve | sumByColumn: 'amount' }}</td>
          <td>{{ test.approve | sumByColumn2: 'reducedAmount' }}</td>
          <td>{{ test.approve | sumByColumn4: 'reducedTotal' }}</td>
        </tr>
        <tr>
          <td colspan="2">Total difference = {{(test.approve | sumByColumn: 'amount') - (test.approve | sumByColumn4: 'reducedTotal')}}</td>
          <td colspan="3">
            <strong>Cumulative Total = #</strong>
          </td>
        </tr>
      </table>
      <form>
        <div class="col-sm-4">
          <div class="form-group">
            <label for="full-name">Name</label>
            <input type="text" class="form-control" id="full-name" placeholder="Enter Full Name">
          </div>
        </div>
        <div class="col-sm-4">
          <div class="form-group">
            <label for="email-address">Email</label>
            <input type="email" class="form-control" id="email-address" aria-describedby="emailHelp" placeholder="Enter email">
          </div>
        </div>
        <div class="col-sm-4">
          <button type="submit" class="btn btn-primary" style="margin-top:25px">Submit</button>
        </div>
      </form>
    </div>
  </section>

</body>        

塑料计算器表格
到目前为止,#地球人已经承诺将他们的一次性塑料废物从{test.approve | sumByColumn:'amount'}{strong>{(test.approve | sumByColumn:'amount')-(test.approve | sumByColumn:'reducedTotal')}减少到{(test approve | sumByColumn:'amount)。这意味着每年减少{test.approve | sumby column4:'reducedTotal'}!尽你的责任。发誓

一次性塑料物品 输入您每周使用的号码 您每年使用的数字为: 你每周能少用多少? 您每年减少的使用量为: {{x.name} {x.number*x.amount} {{x.reducedAmount*x.reducedTotal} 总数 {{test.approve}sumByColumn3:'number'} {{test.approve | sumByColumn:'amount'} {{test.approve}sumByColumn2:'reducedAmount'} {{test.approve | sumby column4:'reducedTotal'} 总差异={(test.approve | sumByColumn:'amount')-(test.approve | sumByColumn4:'reducedTotal')} 累计总额=# 名称 电子邮件 提交

我不确定是否最好使用
window.localStorage
sessionStorage
或者使用PHP。我现在正在试验这个

你的帮助将是宝贵的

我不确定是否最好使用window.localStorage或 sessionStorage,或者用PHP实现。我正在试验这个 现在

这很容易理解。以下是如何:

1.)这些数字(总和)是否与后端数据有关?您是否需要某种服务器代码或功能来执行此操作?如果是:使用php

2.)如果1对您而言为假,您是否希望您的用户在返回时能够再次获取数字或总和?您希望它是持久的吗?然后使用:localStorage

3.)如果2为false,请使用sessionStorage。它不是持久性的,并且在当前会话终止后立即清除。ie:用户关闭选项卡或浏览器


现在,如果您需要一些代码方面的帮助,那么修改您的问题以包含这些细节,或者打开一个新问题,或者简单地对我的答案发表评论。我会尽力帮你的。快乐编码

感谢您提供这些要点,这非常有帮助。我的目标是在用户提交数据后保存并显示总数。因此,当另一个人访问表单时,他们会看到
累计总数=#
比以前增加了。我尝试了
localStorage
方法,但它似乎只在本地保存,只有我能看到,其他人看不到。所以方法#1可能是一条路,我会看看我能想出什么。@Tam当然。听起来不错。我想建议您使用这两种方法来获得更好的用户体验。示例:将您从服务器获得的上一次总值保存到localStorage中,这样即使用户处于脱机状态或无法连接,他/她仍然能够看到最近的一些有意义的内容。再说一次,如果你分享一些代码,我可能会以更具体的方式帮助你,特别是在本地存储以及保存和解释客户端数据方面。谢谢你的提示,因为这个应用程序将生活在WordPress环境中,我用一个叫做Running Totals for Gravity Forms的插件计算出了总数。问题解决了。