javascript - 如何在html表中选择一行乘以2列并在第三列中显示结果
问题描述
我有 HTML 表(带有数据表),其中包含产品价格和产品数量以及总计等列,我想在填充价格和数量时更新总计列。
<table id="example1" class="table table-bordered table-striped dataTable " role="grid" aria-describedby="example1_info">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<?php
while($user = $products->fetch_assoc()){
?>
<tr>
<td><?=$user['id']?></td>
<td><?=$user['name']?></td>
<td>
<input type="number" class="pr price" name="row_id_<?=$user["id"] ?>" pr_id='<?=$user['id']?>' value=""
onchange= "add_to_p_total(this)" style="width: 60px; height: 26px">
</td>
<td>
<input type="number" class="pr qty" name="row_id_<?=$user["id"] ?> pr_id='<?=$user['id']?>' value=""
onchange= "add_to_q_total(this)" style="width: 60px; height: 26px">
</td>
<td>
<input type="number" name="row_id_<?=$user["id"] ?> class="pr total_price" style="width: 60px; height: 26px">
</td>
</tr>
<?
}
?>
<tr>
<td></td>
<td></td>
<td><strong id="p_total"></strong></td>
<td>
<strong id="q_total"></strong>
</td>
</tr>
</tbody>
<tfoot>
<tr>
<th>ID</th>
<th>Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Total</th>
</tr>
</tfoot>
</table>
问题是使用 PHP 和数据库动态生成的表,所以在这种情况下需要某种动态 js 我猜我必须以某种方式访问具有索引和多个 [2] 和 [3] 索引的列。我不知道如何使用 jquery 或 js 来做到这一点。
稍后我还需要使用 AJAX 将这个表数据发送到后端,请建议做这种事情的最佳实践是什么。
解决方案
您不需要多个功能来完成此操作。您可以定位最近的tr元素。然后您可以找到具体的输入来计算该值。
请注意:由于您将值相乘,因此如果输入值为空,则将其1
作为默认值。
演示:
function add_to_total(el){
var parent = $(el).closest('tr');
var price = parent.find('.price').val() == "" ? 1 : parent.find('.price').val();
var qty = parent.find('.qty').val() == "" ? 1 : parent.find('.qty').val();
var total = price * qty;
parent.find('.total_price').val(total);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="example1" class="table table-bordered table-striped dataTable " role="grid" aria-describedby="example1_info">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr>
<td>111</td>
<td>John</td>
<td>
<input type="number" class="pr price" name="" pr_id="" value="" onchange= "add_to_total(this)" style="width: 60px; height: 26px">
</td>
<td>
<input type="number" class="pr qty" name="" value=""
onchange= "add_to_total(this)" style="width: 60px; height: 26px">
</td>
<td>
<input type="number" name="" class="pr total_price" style="width: 60px; height: 26px">
</td>
</tr>
<tr>
<td>222</td>
<td>Jane</td>
<td>
<input type="number" class="pr price" name="" pr_id="" value="" onchange= "add_to_total(this)" style="width: 60px; height: 26px">
</td>
<td>
<input type="number" class="pr qty" name="" value=""
onchange= "add_to_total(this)" style="width: 60px; height: 26px">
</td>
<td>
<input type="number" name="" class="pr total_price" style="width: 60px; height: 26px">
</td>
</tr>
<tr>
<td></td>
<td></td>
<td><strong id="p_total"></strong></td>
<td>
<strong id="q_total"></strong>
</td>
</tr>
</tbody>
<tfoot>
<tr>
<th>ID</th>
<th>Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Total</th>
</tr>
</tfoot>
</table>
推荐阅读
- gcc - 如何理解 PIC 和 no PIC 的区别?
- android-emulator - 模拟器不适用于 AMD Ryzen 和 Windows home
- ios - 如何自定义 UIDocumentPickerViewController 中的图标?
- php - 使用多态关系来表示我网站上的所有页面
- google-cloud-sql - 如何更改 CloudSQL 全局设置
- android - 如何添加自定义大小或编辑应用大小
- python - 如何使用定义的参数构建一系列位置?
- java - 从 S3 下载所有文件并将它们上传到同一文件夹中
- android - CardView内部的Android ConstraintLayout问题
- android - 应用程序崩溃时截取 Android 屏幕截图