如何计算总和:每个胸腺?

发布于 2025-02-08 09:03:45 字数 1420 浏览 2 评论 0 原文

我有一个需要显示的人列表,每个人都有一个年龄的输入。

我需要在我写作时总结一下年龄;我的意思是,如果我在第五张记录时有5个人,我需要这样的4个年龄的总数:通过javascript从数字类型输入字段中计算总和

这是我的代码:

  <tr th:each="person : ${persons}">
            <td th:text="${person.first_name}"></td>
            <td th:text="${person.last_name}"></td>
            <td th:text="${person.phone}"></td>
            <td><input id="age" class="form-control"
                        type="text" name="age" th:field="*{ages}">
            </td>
    </tr>

Total : <input type="text" name="total" id="total"/>

我做了此JS脚本,但总共给我提供了第一行的值!!!!有帮助吗?

<script type="text/javascript">
    $(document).ready(function(){
          $("#age").each(function() {

            $(this).on('input', function(){
              calculateSum();
            });
            
            log.console("sum :"+sum);
          });

        });

        function calculateSum() {

          var sum = 0;
          $("#age").each(function() {
            if(!isNaN(this.value) && this.value.length!=0) {
              sum += parseFloat(this.value);
            }

          });
          $("#sum").html(sum.toFixed(2));

        }
    </script>

I have a list of persons that I need to display, and for each person there is an input for age.

I need to sum the ages when im writing; I mean if I have 5 persons when I'm on the 5th record, i need the total of previous 4 ages like this one : Calculate sum from number type input fields by javascript

this is my code :

  <tr th:each="person : ${persons}">
            <td th:text="${person.first_name}"></td>
            <td th:text="${person.last_name}"></td>
            <td th:text="${person.phone}"></td>
            <td><input id="age" class="form-control"
                        type="text" name="age" th:field="*{ages}">
            </td>
    </tr>

Total : <input type="text" name="total" id="total"/>

I did this js script, but in total it gives me only the value of first line !!!! Any help ?

<script type="text/javascript">
    $(document).ready(function(){
          $("#age").each(function() {

            $(this).on('input', function(){
              calculateSum();
            });
            
            log.console("sum :"+sum);
          });

        });

        function calculateSum() {

          var sum = 0;
          $("#age").each(function() {
            if(!isNaN(this.value) && this.value.length!=0) {
              sum += parseFloat(this.value);
            }

          });
          $("#sum").html(sum.toFixed(2));

        }
    </script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断爱 2025-02-15 09:03:45
  1. 将所有年龄段放在
  2. 您想显示以前年龄的总和的每个地方:

2.1。将数组从0切成当前值的位置(BeaCuse slice(0,x)实际上将数组从0切成0到X-1)。

2.2在切片阵列上执行降低功能,您可以在此处看到一个示例:

  1. put all of the ages in array
  2. On every place you want to show the sum of the previous ages:

2.1. slice the array from 0 to the place of the current value (beacuse slice(0,x) actually slicing the array from 0 to x-1).

2.2 execute the reduce function on the sliced array, you can see an example here: https://www.w3schools.com/jsref/jsref_reduce.asp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文