gpt4 book ai didi

javascript - 如何从行总计中获取总计

转载 作者:行者123 更新时间:2023-12-03 00:51:01 25 4
gpt4 key购买 nike

我是 JavaScript 新手。我有一张包含产品价格、数量、宽度和高度的表格。我的发票中有一行,有一个用于添加新行和删除行的按钮。我分别得到了每一行的总数。现在我想从所有产品的总计中获取总计。添加产品总计后,我想将此发票存储在数据库中。请给我一个更好的解决方案。提前致谢。问候

<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
table,tr,td,th { border: 1px black solid;}

</style>
</head>

<body>
<table>
<thead>
<th>Price</th>
<th>Quantity</th>
<th>Width</th>
<th>Height</th>
<th>Total</th>
<th>Action</th>
</thead>

<tbody id="product_table">
<tr>
<td><input type="text" name="price"></td>
<td><input type="text" name="quantity"></td>
<td><input type="text" name="width"></td>
<td><input type="text" name="height"></td>
<td><input type="text" name="total" readonly></td>
<td><input type="button" value="X" onclick="deleteRow(this)"/></td>
</tr>

</tbody>
<input type="button" name="submit" value="Add Row" onclick="add_fields();">

</table>
<span>Grand Total<input type="text" name="grandtotal" readonly></span>
</body>

<script>

const table = document.getElementById('product_table');
table.addEventListener('input', ({ target }) => {
const tr = target.closest('tr');
const [price, quantity, width, height, total] = tr.querySelectorAll('input');

var size = width.value * height.value;
var rate = price.value * quantity.value;

if (size != "") {
total.value = size * rate;
}else{
total.value = rate;
}
});

function add_fields() {
var row = document.createElement("tr");
row.innerHTML =
'<td><input type="text" name="price"></td>' +
'<td><input type="text" name="quantity"></td>' +
'<td><input type="text" name="width"></td>' +
'<td><input type="text" name="height"></td>' +
'<td><input type="text" name="total" readonly></td>' +
'<td><input type="button" value="X" onclick="deleteRow(this)"/></td>';

table.appendChild(row);
}

function deleteRow(btn) {
var row = btn.parentNode.parentNode;
row.parentNode.removeChild(row);
}

</script>
</html>

最佳答案

这是您的工作代码。

<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js</script>
<style>
table,tr,td,th { border: 1px black solid;}

</style>
</head>

<body>
<table>
<thead>
<th>Price</th>
<th>Quantity</th>
<th>Width</th>
<th>Height</th>
<th>Total</th>
<th>Action</th>
</thead>

<tbody id="product_table">
<tr>
<td><input type="text" name="price"></td>
<td><input type="text" name="quantity"></td>
<td><input type="text" name="width"></td>
<td><input type="text" name="height"></td>
<td><input type="text" name="total" class="totalPrice" readonly></td>
<td><input type="button" value="X" onclick="deleteRow(this)"/></td>
</tr>

</tbody>
<input type="button" name="submit" value="Add Row" onclick="add_fields();">

</table>
<span>Grand Total<input type="text" name="grandtotal" id="grandtotal" readonly></span>
</body>

<script>

const table = document.getElementById('product_table');
table.addEventListener('input', ({ target }) => {
const tr = target.closest('tr');
const [price, quantity, width, height, total] = tr.querySelectorAll('input');

var size = width.value * height.value;
var rate = price.value * quantity.value;

if (size != "") {
total.value = size * rate;
}else{
total.value = rate;
}
totalPrice();
});

function add_fields() {
var row = document.createElement("tr");
row.innerHTML =
'<td><input type="text" name="price"></td>' +
'<td><input type="text" name="quantity"></td>' +
'<td><input type="text" name="width"></td>' +
'<td><input type="text" name="height"></td>' +
'<td><input type="text" name="total" class="totalPrice" readonly></td>' +
'<td><input type="button" value="X" onclick="deleteRow(this)"/></td>';

table.appendChild(row);
}

function deleteRow(btn) {
var row = btn.parentNode.parentNode;
row.parentNode.removeChild(row);
totalPrice();
}
function totalPrice(){
var sum = 0;
$(".totalPrice").each(function(){
sum += parseFloat($(this).val());
});
$("#grandtotal").val(sum);
}

</script>
</html>

希望它对您有用。

查看此演示:https://jsfiddle.net/8jo7u4ya/

关于javascript - 如何从行总计中获取总计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53043156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com