您如何存储总计,并在每次用户点击提交时累计?

例如,每当用户提交信息时,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>


Link to Pen

我不确定是否最好使用window.localStoragesessionStorage或最好使用PHP。我现在正在尝试。

您的帮助将是黄金。

最佳答案

我不确定是否最好与window.localStorage或
  sessionStorage或使用PHP完成。我正在尝试
  现在。


弄清楚这一点很简单。方法如下:

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

2.)如果1对您来说是错误的,您是否希望用户再次回来时能够重新获得数字或总和?您想要它持久吗?然后使用:localStorage

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

现在,如果您需要代码方面的帮助,请修改您的问题以包括这些详细信息,或者打开一个新问题,或者只是对我的答案发表评论。我会尽力帮助您。编码愉快!

关于javascript - 存储总数,并在提交时累计,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57168160/

10-11 06:10