php - 如何将值计算到一个输入字段中?
问题描述
我有两个 td 值的表
<td><?php echo $row['remun'];?></td>
<td><?php echo $row['lum'];?></td>
它根据行值返回一些值
for example
Ist column 2nd column
100 150
200 150
300 150
<input type="text" name="grand_total" id="grand_total" />
javascript
<script>
$('#dynamic-table tr').each(function() {
if (!this.rowIndex) return; // skip first row
var customerId = this.cells[4].innerHTML;
alert(customerId);
$("#grand_total").val(customerId);
});
我只得到第一行的值,但我想要所有字段的总和。
解决方案
但是你可以很容易地从你的 php 循环中管理它。当然,您正在使用 for 循环来打印表中的记录。
<?php
$grand_total=0;
foreach($a as $row){
$grand_total=$grand_total+$row['remun']+$row['lum'];
?>
<td class="remun"><?php echo $row['remun'];?></td>
<td class="lum"><?php echo $row['lum'];?></td>
<?php }?>
<td class="total"><?php echo $grand_total;?></td>
但是如果你想用 js 来做,那么请按照下面的操作一次。请在下面添加一些类。
<td class="remun"><?php echo $row['remun'];?></td>
<td class="lum"><?php echo $row['lum'];?></td>
现在用户在 jquery 代码下方获取两者的总和。
var t1=0;
var t2=0;
var t3=0;
$('.remun').each(function(){
t1=t1+parseInt($(this).html());
});
$('.lum').each(function(){
t2=t2+parseInt($(this).html());
});
t3=t1+t2;
alert(t3);
推荐阅读
- javascript - 关于 Promise Then
- pandas - 第二个 csv 文件的长度不等于使用 np.select 的基本文件
- python - 组内连续行与创建相同的字符串之间的数据帧差异
- jms - 我们如何阻止 ibm mq amqp 订阅者访问其他订阅者的主题?
- python - 解析熊猫中的特定日期和时间顺序
- user-agent - Microsoft Surface Pro 平板电脑的用户代理字符串
- php - PHPMailer文件上传是正确的
- c# - 从 Unloaded 事件处理程序引发的 RoutedEvent 未冒泡
- postgresql - 双引号在更新查询中给出问题
- javascript - 用百分比计算器创建一个整数