如何计算总和:每个胸腺?
我有一个需要显示的人列表,每个人都有一个年龄的输入。
我需要在我写作时总结一下年龄;我的意思是,如果我在第五张记录时有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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
2.1。将数组从0切成当前值的位置(BeaCuse slice(0,x)实际上将数组从0切成0到X-1)。
2.2在切片阵列上执行降低功能,您可以在此处看到一个示例:
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