英文:
How to update the total number immediately when i put the value in the table
问题
I have translated the code for you as requested. Here is the translated code:
<!-- 开始代码片段: js 隐藏: false 控制台: false Babel: false -->
<!-- 语言: lang-js -->
function calculateTotal() {
let rows = document.querySelectorAll("tbody tr");
let grandTotal = 0;
let negative = false;
for (let i = 0; i < rows.length; i++) {
let row = rows[i];
let quantityInput = row.querySelector(".quantity input");
let priceInput = row.querySelector(".price input");
let totalInput = row.querySelector(".total input");
let quantity = parseInt(quantityInput.value);
let price = parseFloat(priceInput.value).toFixed(2);
if (quantity < 0 || price < 0) {
negative = true;
quantity = 0;
price = "0.00";
quantityInput.value = "0";
priceInput.value = "0.00";
}
let total = quantity * price;
totalInput.value = total.toFixed(2);
grandTotal += total;
}
document.querySelector(".GrandTotal").value = grandTotal.toFixed(2);
if (negative) {
alert("不允许负值!");
}
}
<!-- 语言: lang-css -->
table, td {
padding: 0.5rem;
border: 3px solid;
border-collapse: collapse;
}
h1 {
font-family: 'Ubuntu', cursive;
}
th {
background-color: skyblue;
font-weight: bold;
padding: 0.5rem;
border: 3px solid;
border-collapse: collapse;
}
tbody {
background-color: white;
}
.No {
text-align: right;
}
td:nth-child(7) input[type="number"] {
text-align: right;
background-color: silver;
}
td:nth-child(6) input[type="number"] {
text-align: right;
}
td:nth-child(5) input[type="number"] {
text-align: right;
}
tfoot tr td:last-child input[type="number"] {
text-align: right;
}
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
tfoot {
font-weight: bold;
}
.Button {
float: right;
}
.GrandTotal {
background-color: silver;
font-size: 18pt;
float: right;
}
tr:hover {
background-color: yellow;
}
.background-colour {
background-color: skyblue;
}
body {
background-color: lemonchiffon;
}
<!-- 语言: lang-html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>图书订购系统</title>
<link rel="stylesheet" href="CSS/book-order.css">
<script src="book-order.js"></script>
</head>
<body>
<h1>图书订购系统</h1>
<table id="myTable">
<thead>
<tr>
<th>编号</th>
<th>书名</th>
<th>作者</th>
<th>类别</th>
<th>单价</th>
<th>数量</th>
<th>总计</th>
</tr>
</thead>
<tbody>
<tr>
<td class="No">1</td>
<!-- 书名 -->
<td><input type="text" value=""></td>
<!-- 作者 -->
<td><input type="text" value=""></td>
<!-- 类别 -->
<td>
<select>
<option value="请选择类别..." disabled selected>请选择类别...</option>
<option value="商业">商业</option>
<option value="小说">小说</option>
<option value="数学">数学</option>
<option value="技术">技术</option>
</select>
</td>
<!-- 单价 -->
<td class="price"><input type="number" value="0.00"></td>
<!-- 数量 -->
<td class="quantity"><input type="number" value="0"></td>
<!-- 总计 -->
<td class="total"><input type="number" value="0.00" disabled></td>
</tr>
<tr>
<td class="No">2</td>
<!-- 书名 -->
<td><input type="text" value=""></td>
<!-- 作者 -->
<td><input type="text" value=""></td>
<!-- 类别 -->
<td>
<select>
<option value="请选择类别..." disabled selected>请选择类别...</option>
<option value="商业">商业</option>
<option value="小说">小说</option>
<option value="数学">数学</option>
<option value="技术">技术</option>
</select>
</td>
<!-- 单价 -->
<td class="price"><input type="number" value="0.00"></td>
<!-- 数量 -->
<td class="quantity"><input type="number" value="0"></td>
<!-- 总计 -->
<td class="total"><input type="number" value="0.00" disabled></td>
</tr>
<tr>
<td class="No">3</td>
<!-- 书名 -->
<td><input type="text" value=""></td>
<!-- 作者 -->
<td><input type="text" value=""></td>
<!-- 类别 -->
<td>
<select>
<option value="请选择类别..." disabled selected>请选择类别...</option>
<option value="商业">商业</option>
<option value="小说">小说</option>
<option value="数学">数学</option>
<option value="技术">技术</option>
</select>
</td>
<!-- 单价 -->
<td class="price"><input type="number" value="0.00"></td>
<!-- 数量 -->
<td class="quantity"><input type="number" value="0"></td>
<!-- 总计 -->
<td class="total"><input type="number" value="0.00" disabled></td>
</tr>
<tr>
<td class="No">4</td>
<!-- 书名 -->
<td><input type="text" value=""></td>
<!-- 作者 -->
<td><input type="text" value=""></td>
<!-- 类别 -->
<td>
<select>
<option value="请选择类别..." disabled
<details>
<summary>英文:</summary>
<!-- begin snippet: js hide: false console: false babel: false -->
<!-- language: lang-js -->
function calculateTotal() {
let rows = document.querySelectorAll("tbody tr");
let grandTotal = 0;
let negative = false;
for (let i = 0; i < rows.length; i++) {
let row = rows[i];
let quantityInput = row.querySelector(".quantity input");
let priceInput = row.querySelector(".price input");
let totalInput = row.querySelector(".total input");
let quantity = parseInt(quantityInput.value);
let price = parseFloat(priceInput.value).toFixed(2);
if(quantity < 0 || price < 0){
negative = true;
quantity = 0;
price = 0.00;
quantityInput.value = "0";
priceInput.value = "0.00";
}
let total = quantity * price;
totalInput.value = total.toFixed(2);
grandTotal += total;
}
document.querySelector(".GrandTotal").value = grandTotal.toFixed(2);
if(negative){
alert("No Negative Values!")
}
}
<!-- language: lang-css -->
table,td {
padding: 0.5rem;
border: 3px solid;
border-collapse:collapse;
}
h1 {
font-family: 'Ubuntu', cursive;
}
th {
background-color: skyblue;
font-weight: bold;
padding: 0.5rem;
border: 3px solid;
border-collapse:collapse;
}
tbody {
background-color: white;
}
.No {
text-align: right;
}
td:nth-child(7) input[type="number"]{
text-align: right; background-color: silver;
}
td:nth-child(6) input[type="number"]{
text-align: right;
}
td:nth-child(5) input[type="number"]{
text-align: right;
}
tfoot tr td:last-child input[type="number"]{
text-align: right;
}
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
tfoot {
font-weight: bold;
}
.Button {
float:right;
}
.GrandTotal {
background-color: silver;
font-size: 18pt;
float:right;
}
tr:hover {
background-color: yellow;
}
.background-colour {
background-color: skyblue;
}
body {
background-color: lemonchiffon;
}
<!-- language: lang-html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Book Ordering System</title>
<link rel="stylesheet" href="CSS/book-order.css">
<script src="book-order.js"></script>
</head>
<body>
<h1>Book Ordering System</h1>
<table id="myTable">
<thead>
<tr>
<th>No.</th>
<th>Book Title</th>
<th>Author</th>
<th>Category</th>
<th>Unit Price</th>
<th>Quantity</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr>
<td class="No">1</td>
<!--Book Title-->
<td><input type="text" value=""></td>
<!--Author-->
<td><input type="text" value=""></td>
<!--Category-->
<td>
<select>
<option value="Please choose the category..." disabled selected>Please choose the category...</option>
<option value="Business">Business</option>
<option value="Fiction">Fiction</option>
<option value="Mathematics">Mathematics</option>
<option value="Technology">Technology</option>
</select>
</td>
<!--Unit Price-->
<td class="price"><input type="number" value="0.00"></td>
<!--Quantity-->
<td class="quantity"><input type="number" value="0"></td>
<!--Total-->
<td class="total"><input type="number" value="0.00" disabled></td>
</tr>
<tr>
<td class="No">2</td>
<!--Book Title-->
<td><input type="text" value=""></td>
<!--Author-->
<td><input type="text" value=""></td>
<!--Category-->
<td>
<select>
<option value="Please choose the category..." disabled selected>Please choose the category...</option>
<option value="Business">Business</option>
<option value="Fiction">Fiction</option>
<option value="Mathematics">Mathematics</option>
<option value="Technology">Technology</option>
</select>
</td>
<!--Unit Price-->
<td class="price"><input type="number" value="0.00"></td>
<!--Quantity-->
<td class="quantity"><input type="number" value="0"></td>
<!--Total-->
<td class="total"><input type="number" value="0.00" disabled></td>
</tr>
<tr>
<td class="No">3</td>
<!--Book Title-->
<td><input type="text" value=""></td>
<!--Author-->
<td><input type="text" value=""></td>
<!--Category-->
<td>
<select>
<option value="Please choose the category..." disabled selected>Please choose the category...</option>
<option value="Business">Business</option>
<option value="Fiction">Fiction</option>
<option value="Mathematics">Mathematics</option>
<option value="Technology">Technology</option>
</select>
</td>
<!--Unit Price-->
<td class="price"><input type="number" value="0.00"></td>
<!--Quantity-->
<td class="quantity"><input type="number" value="0"></td>
<!--Total-->
<td class="total"><input type="number" value="0.00" disabled></td>
</tr>
<tr>
<td class="No">4</td>
<!--Book Title-->
<td><input type="text" value=""></td>
<!--Author-->
<td><input type="text" value=""></td>
<!--Category-->
<td>
<select>
<option value="Please choose the category..." disabled selected>Please choose the category...</option>
<option value="Business">Business</option>
<option value="Fiction">Fiction</option>
<option value="Mathematics">Mathematics</option>
<option value="Technology">Technology</option>
</select>
</td>
<!--Unit Price-->
<td class="price"><input type="number" value="0.00"></td>
<!--Quantity-->
<td class="quantity"><input type="number" value="0"></td>
<!--Total-->
<td class="total"><input type="number" value="0.00" disabled></td>
</tr>
<tr>
<td class="No">5</td>
<!--Book Title-->
<td><input type="text" value=""></td>
<!--Author-->
<td><input type="text" value=""></td>
<!--Category-->
<td>
<select>
<option value="Please choose the category..." disabled selected>Please choose the category...</option>
<option value="Business">Business</option>
<option value="Fiction">Fiction</option>
<option value="Mathematics">Mathematics</option>
<option value="Technology">Technology</option>
</select>
</td>
<!--Unit Price-->
<td class="price"><input type="number" value="0.00"></td>
<!--Quantity-->
<td class="quantity"><input type="number" value="0"></td>
<!--Total-->
<td class="total"><input type="number" value="0.00" disabled></td>
</tr>
</tbody>
<tfoot>
<tr>
<td class="background-colour" colspan="5">
<button type="button" onclick="calculateTotal()" class="Button">Calculate Grand Total Price</button>
</td>
<td class="background-colour" colspan="2">
<input type="number" name="Grand Total Price" value="0.00" class="GrandTotal" disabled>
</td>
</tr>
</tfoot>
</table>
</body>
</html>
<!-- end snippet -->
I am trying to update the total value in the table immediately right after I input the value in unit price and quantity without clicking the button. The purpose of the "Calculate Grand Total Price" button is suppose to sum all of the total and display it in "GrandTotal" input field only.
This is my HTML code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Book Ordering System</title>
<link rel="stylesheet" href="CSS/book-order.css">
<script src="book-order.js"></script>
</head>
<body>
<h1>Book Ordering System</h1>
<table id="myTable">
<thead>
<tr>
<th>No.</th>
<th>Book Title</th>
<th>Author</th>
<th>Category</th>
<th>Unit Price</th>
<th>Quantity</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr>
<td class="No">1</td>
<!--Book Title-->
<td><input type="text" value=""></td>
<!--Author-->
<td><input type="text" value=""></td>
<!--Category-->
<td>
<select>
<option value="Please choose the category..." disabled selected>Please choose the category...</option>
<option value="Business">Business</option>
<option value="Fiction">Fiction</option>
<option value="Mathematics">Mathematics</option>
<option value="Technology">Technology</option>
</select>
</td>
<!--Unit Price-->
<td class="price"><input type="number" value="0.00"></td>
<!--Quantity-->
<td class="quantity"><input type="number" value="0"></td>
<!--Total-->
<td class="total"><input type="number" value="0.00" disabled></td>
</tr>
<tr>
<td class="No">2</td>
<!--Book Title-->
<td><input type="text" value=""></td>
<!--Author-->
<td><input type="text" value=""></td>
<!--Category-->
<td>
<select>
<option value="Please choose the category..." disabled selected>Please choose the category...</option>
<option value="Business">Business</option>
<option value="Fiction">Fiction</option>
<option value="Mathematics">Mathematics</option>
<option value="Technology">Technology</option>
</select>
</td>
<!--Unit Price-->
<td class="price"><input type="number" value="0.00"></td>
<!--Quantity-->
<td class="quantity"><input type="number" value="0"></td>
<!--Total-->
<td class="total"><input type="number" value="0.00" disabled></td>
</tr>
<tr>
<td class="No">3</td>
<!--Book Title-->
<td><input type="text" value=""></td>
<!--Author-->
<td><input type="text" value=""></td>
<!--Category-->
<td>
<select>
<option value="Please choose the category..." disabled selected>Please choose the category...</option>
<option value="Business">Business</option>
<option value="Fiction">Fiction</option>
<option value="Mathematics">Mathematics</option>
<option value="Technology">Technology</option>
</select>
</td>
<!--Unit Price-->
<td class="price"><input type="number" value="0.00"></td>
<!--Quantity-->
<td class="quantity"><input type="number" value="0"></td>
<!--Total-->
<td class="total"><input type="number" value="0.00" disabled></td>
</tr>
<tr>
<td class="No">4</td>
<!--Book Title-->
<td><input type="text" value=""></td>
<!--Author-->
<td><input type="text" value=""></td>
<!--Category-->
<td>
<select>
<option value="Please choose the category..." disabled selected>Please choose the category...</option>
<option value="Business">Business</option>
<option value="Fiction">Fiction</option>
<option value="Mathematics">Mathematics</option>
<option value="Technology">Technology</option>
</select>
</td>
<!--Unit Price-->
<td class="price"><input type="number" value="0.00"></td>
<!--Quantity-->
<td class="quantity"><input type="number" value="0"></td>
<!--Total-->
<td class="total"><input type="number" value="0.00" disabled></td>
</tr>
<tr>
<td class="No">5</td>
<!--Book Title-->
<td><input type="text" value=""></td>
<!--Author-->
<td><input type="text" value=""></td>
<!--Category-->
<td>
<select>
<option value="Please choose the category..." disabled selected>Please choose the category...</option>
<option value="Business">Business</option>
<option value="Fiction">Fiction</option>
<option value="Mathematics">Mathematics</option>
<option value="Technology">Technology</option>
</select>
</td>
<!--Unit Price-->
<td class="price"><input type="number" value="0.00"></td>
<!--Quantity-->
<td class="quantity"><input type="number" value="0"></td>
<!--Total-->
<td class="total"><input type="number" value="0.00" disabled></td>
</tr>
</tbody>
<tfoot>
<tr>
<td class="background-colour" colspan="5">
<button type="button" onclick="calculateTotal()" class="Button">Calculate Grand Total Price</button>
</td>
<td class="background-colour" colspan="2">
<input type="number" name="Grand Total Price" value="0.00" class="GrandTotal" disabled>
</td>
</tr>
</tfoot>
</table>
</body>
</html>
This is my CSS code:
table,td {
padding: 0.5rem;
border: 3px solid;
border-collapse:collapse;
}
h1 {
font-family: 'Ubuntu', cursive;
}
th {
background-color: skyblue;
font-weight: bold;
padding: 0.5rem;
border: 3px solid;
border-collapse:collapse;
}
tbody {
background-color: white;
}
.No {
text-align: right;
}
td:nth-child(7) input[type="number"]{
text-align: right; background-color: silver;
}
td:nth-child(6) input[type="number"]{
text-align: right;
}
td:nth-child(5) input[type="number"]{
text-align: right;
}
tfoot tr td:last-child input[type="number"]{
text-align: right;
}
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
tfoot {
font-weight: bold;
}
.Button {
float:right;
}
.GrandTotal {
background-color: silver;
font-size: 18pt;
float:right;
}
tr:hover {
background-color: yellow;
}
.background-colour {
background-color: skyblue;
}
body {
background-color: lemonchiffon;
}
This is my Javascript :
function calculateTotal() {
let rows = document.querySelectorAll("tbody tr");
let grandTotal = 0;
let negative = false;
for (let i = 0; i < rows.length; i++) {
let row = rows[i];
let quantityInput = row.querySelector(".quantity input");
let priceInput = row.querySelector(".price input");
let totalInput = row.querySelector(".total input");
let quantity = parseInt(quantityInput.value);
let price = parseFloat(priceInput.value).toFixed(2);
if(quantity < 0 || price < 0){
negative = true;
quantity = 0;
price = 0.00;
quantityInput.value = "0";
priceInput.value = "0.00";
}
let total = quantity * price;
totalInput.value = total.toFixed(2);
grandTotal += total;
}
document.querySelector(".GrandTotal").value = grandTotal.toFixed(2);
if(negative){
alert("No Negative Values!")
}
}
</details>
# 答案1
**得分**: 1
以下是您要翻译的内容:
```html
<h1>图书订购系统</h1>
<table id="myTable">
<thead>
<tr>
<th>编号</th>
<th>图书标题</th>
<th>作者</th>
<th>类别</th>
<th>单价</th>
<th>数量</th>
<th>总计</th>
</tr>
</thead>
<tbody>
<tr>
<td class="No">1</td>
<!--图书标题-->
<td><input type="text" value=""></td>
<!--作者-->
<td><input type="text" value=""></td>
<!--类别-->
<td>
<select>
<option value="请选择类别..." disabled selected>请选择类别...</option>
<option value="商业">商业</option>
<option value="小说">小说</option>
<option value="数学">数学</option>
<option value="技术">技术</option>
</select>
</td>
<!--单价-->
<td class="price"><input type="number" value="0.00"></td>
<!--数量-->
<td class="quantity"><input type="number" value="0"></td>
<!--总计-->
<td class="total"><input type="number" value="0.00" disabled></td>
</tr>
<tr>
<td class="No">2</td>
<!--图书标题-->
<td><input type="text" value=""></td>
<!--作者-->
<td><input type="text" value=""></td>
<!--类别-->
<td>
<select>
<option value="请选择类别..." disabled selected>请选择类别...</option>
<option value="商业">商业</option>
<option value="小说">小说</option>
<option value="数学">数学</option>
<option value="技术">技术</option>
</select>
</td>
<!--单价-->
<td class="price"><input type="number" value="0.00"></td>
<!--数量-->
<td class="quantity"><input type="number" value="0"></td>
<!--总计-->
<td class="total"><input type="number" value="0.00" disabled></td>
</tr>
<tr>
<td class="No">3</td>
<!--图书标题-->
<td><input type="text" value=""></td>
<!--作者-->
<td><input type="text" value=""></td>
<!--类别-->
<td>
<select>
<option value="请选择类别..." disabled selected>请选择类别...</option>
<option value="商业">商业</option>
<option value="小说">小说</option>
<option value="数学">数学</option>
<option value="技术">技术</option>
</select>
</td>
<!--单价-->
<td class="price"><input type="number" value="0.00"></td>
<!--数量-->
<td class="quantity"><input type="number" value="0"></td>
<!--总计-->
<td class="total"><input type="number" value="0.00" disabled></td>
</tr>
<tr>
<td class="No">4</td>
<!--图书标题-->
<td><input type="text" value=""></td>
<!--作者-->
<td><input type="text" value=""></td>
<!--类别-->
<td>
<select>
<option value="请选择类别..." disabled selected>请选择类别...</option>
<option value="商业">商业</option>
<option value="小说">小说</option>
<option value="数学">数学</option>
<option value="技术">技术</option>
</select>
</td>
<!--单价-->
<td class="price"><input type="number" value="0.00"></td>
<!--数量-->
<td class="quantity"><input type="number" value="0"></td>
<!--总计-->
<td class="total"><input type="number" value="0.00" disabled></td>
</tr>
<tr>
<td class="No">5</td>
<!--图书标题-->
<td><input type="text" value=""></td>
<!--作者-->
<td><input type="text" value=""></td>
<!--类别-->
<td>
<select>
<option value="请选择类别..." disabled selected>请选择类别...</option>
<option value="商业">商业</option>
<option value="小说">小说</option>
<option value="数学">数学</option>
<option value="技术">技术</option>
</select>
</td>
<!--单价-->
<td class="price"><input type="number" value="0.00"></td>
<!--数量-->
<td class="quantity"><input type="number" value="0"></td>
<!--总计-->
<td class="total"><input type="number" value="0.00" disabled></td>
</tr>
</tbody>
<tfoot>
<tr>
<td class="background-colour" colspan="5">
</td>
<td class="background-colour" colspan="2">
<input type="number" name="总计价格" value="0.00" class="GrandTotal" disabled>
</td>
</tr>
</tfoot>
</table>
请注意,我将"Grand Total Price"翻译为"总计价格",以便更好地适应中文环境。如果需要其他翻译或有其他问题,请告诉我。
英文:
Delegate
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
window.addEventListener("DOMContentLoaded", () => { // when the page hass loaded
document.getElementById("myTable").addEventListener("input", () => { // any input - you can test if it is a number field if you wish
let rows = document.querySelectorAll("tbody tr");
let negative = false;
let grandTotal = [...rows].map(row => {
let quantityInput = row.querySelector(".quantity input");
let priceInput = row.querySelector(".price input");
let totalInput = row.querySelector(".total input");
let quantity = parseInt(quantityInput.value);
let price = parseFloat(priceInput.value).toFixed(2);
if (quantity < 0 || price < 0) {
negative = true;
quantity = 0;
price = 0.00;
quantityInput.value = "0";
priceInput.value = "0.00";
}
let total = quantity * price;
totalInput.value = total.toFixed(2);
return total;
}).reduce((a, b) => a + b)
document.querySelector(".GrandTotal").value = grandTotal.toFixed(2);
if (negative) {
alert("No Negative Values!")
}
});
});
<!-- language: lang-html -->
<h1>Book Ordering System</h1>
<table id="myTable">
<thead>
<tr>
<th>No.</th>
<th>Book Title</th>
<th>Author</th>
<th>Category</th>
<th>Unit Price</th>
<th>Quantity</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr>
<td class="No">1</td>
<!--Book Title-->
<td><input type="text" value=""></td>
<!--Author-->
<td><input type="text" value=""></td>
<!--Category-->
<td>
<select>
<option value="Please choose the category..." disabled selected>Please choose the category...</option>
<option value="Business">Business</option>
<option value="Fiction">Fiction</option>
<option value="Mathematics">Mathematics</option>
<option value="Technology">Technology</option>
</select>
</td>
<!--Unit Price-->
<td class="price"><input type="number" value="0.00"></td>
<!--Quantity-->
<td class="quantity"><input type="number" value="0"></td>
<!--Total-->
<td class="total"><input type="number" value="0.00" disabled></td>
</tr>
<tr>
<td class="No">2</td>
<!--Book Title-->
<td><input type="text" value=""></td>
<!--Author-->
<td><input type="text" value=""></td>
<!--Category-->
<td>
<select>
<option value="Please choose the category..." disabled selected>Please choose the category...</option>
<option value="Business">Business</option>
<option value="Fiction">Fiction</option>
<option value="Mathematics">Mathematics</option>
<option value="Technology">Technology</option>
</select>
</td>
<!--Unit Price-->
<td class="price"><input type="number" value="0.00"></td>
<!--Quantity-->
<td class="quantity"><input type="number" value="0"></td>
<!--Total-->
<td class="total"><input type="number" value="0.00" disabled></td>
</tr>
<tr>
<td class="No">3</td>
<!--Book Title-->
<td><input type="text" value=""></td>
<!--Author-->
<td><input type="text" value=""></td>
<!--Category-->
<td>
<select>
<option value="Please choose the category..." disabled selected>Please choose the category...</option>
<option value="Business">Business</option>
<option value="Fiction">Fiction</option>
<option value="Mathematics">Mathematics</option>
<option value="Technology">Technology</option>
</select>
</td>
<!--Unit Price-->
<td class="price"><input type="number" value="0.00"></td>
<!--Quantity-->
<td class="quantity"><input type="number" value="0"></td>
<!--Total-->
<td class="total"><input type="number" value="0.00" disabled></td>
</tr>
<tr>
<td class="No">4</td>
<!--Book Title-->
<td><input type="text" value=""></td>
<!--Author-->
<td><input type="text" value=""></td>
<!--Category-->
<td>
<select>
<option value="Please choose the category..." disabled selected>Please choose the category...</option>
<option value="Business">Business</option>
<option value="Fiction">Fiction</option>
<option value="Mathematics">Mathematics</option>
<option value="Technology">Technology</option>
</select>
</td>
<!--Unit Price-->
<td class="price"><input type="number" value="0.00"></td>
<!--Quantity-->
<td class="quantity"><input type="number" value="0"></td>
<!--Total-->
<td class="total"><input type="number" value="0.00" disabled></td>
</tr>
<tr>
<td class="No">5</td>
<!--Book Title-->
<td><input type="text" value=""></td>
<!--Author-->
<td><input type="text" value=""></td>
<!--Category-->
<td>
<select>
<option value="Please choose the category..." disabled selected>Please choose the category...</option>
<option value="Business">Business</option>
<option value="Fiction">Fiction</option>
<option value="Mathematics">Mathematics</option>
<option value="Technology">Technology</option>
</select>
</td>
<!--Unit Price-->
<td class="price"><input type="number" value="0.00"></td>
<!--Quantity-->
<td class="quantity"><input type="number" value="0"></td>
<!--Total-->
<td class="total"><input type="number" value="0.00" disabled></td>
</tr>
</tbody>
<tfoot>
<tr>
<td class="background-colour" colspan="5">
</td>
<td class="background-colour" colspan="2">
<input type="number" name="Grand Total Price" value="0.00" class="GrandTotal" disabled>
</td>
</tr>
</tfoot>
</table>
<!-- end snippet -->
答案2
得分: 0
以下是您提供的代码的翻译:
您可以在所有输入元素上添加一个事件监听器:
<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->
<!-- 语言:lang-js -->
// 获取所有输入元素
let inputs = document.querySelectorAll("input");
let grandTotal = 0;
inputs.forEach((input) => input.addEventListener('input', () => calculateTotal()))
function calculateTotal() {
let rows = document.querySelectorAll("tbody tr");
grandTotal = 0
let negative = false;
for (let i = 0; i < rows.length; i++) {
let row = rows[i];
let quantityInput = row.querySelector(".quantity input");
let priceInput = row.querySelector(".price input");
let totalInput = row.querySelector(".total input");
let quantity = parseInt(quantityInput.value);
let price = parseFloat(priceInput.value).toFixed(2);
if (quantity < 0 || price < 0) {
negative = true;
quantity = 0;
price = 0.00;
quantityInput.value = "0";
priceInput.value = "0.00";
}
let total = quantity * price;
totalInput.value = total.toFixed(2);
grandTotal += total;
}
if (negative) {
alert("不允许负值!")
}
}
function updateGrandTotal () {
document.querySelector(".GrandTotal").value = grandTotal.toFixed(2);
}
<!-- 语言:lang-html -->
<h1>图书订购系统</h1>
<table id="myTable">
<thead>
<tr>
<th>编号</th>
<th>书名</th>
<th>作者</th>
<th>类别</th>
<th>单价</th>
<th>数量</th>
<th>总计</th>
</tr>
</thead>
<tbody>
<tr>
<td class="No">1</td>
<!-- 书名 -->
<td><input type="text" value=""></td>
<!-- 作者 -->
<td><input type="text" value=""></td>
<!-- 类别 -->
<td>
<select>
<option value="请选择类别..." disabled selected>请选择类别...</option>
<option value="商业">商业</option>
<option value="小说">小说</option>
<option value="数学">数学</option>
<option value="技术">技术</option>
</select>
</td>
<!-- 单价 -->
<td class="price"><input type="number" value="0.00"></td>
<!-- 数量 -->
<td class="quantity"><input type="number" value="0"></td>
<!-- 总计 -->
<td class="total"><input type="number" value="0.00" disabled></td>
</tr>
<tr>
<td class="No">2</td>
<!-- 书名 -->
<td><input type="text" value=""></td>
<!-- 作者 -->
<td><input type="text" value=""></td>
<!-- 类别 -->
<td>
<select>
<option value="请选择类别..." disabled selected>请选择类别...</option>
<option value="商业">商业</option>
<option value="小说">小说</option>
<option value="数学">数学</option>
<option value="技术">技术</option>
</select>
</td>
<!-- 单价 -->
<td class="price"><input type="number" value="0.00"></td>
<!-- 数量 -->
<td class="quantity"><input type="number" value="0"></td>
<!-- 总计 -->
<td class="total"><input type="number" value="0.00" disabled></td>
</tr>
<tr>
<td class="No">3</td>
<!-- 书名 -->
<td><input type="text" value=""></td>
<!-- 作者 -->
<td><input type="text" value=""></td>
<!-- 类别 -->
<td>
<select>
<option value="请选择类别..." disabled selected>请选择类别...</option>
<option value="商业">商业</option>
<option value="小说">小说</option>
<option value="数学">数学</option>
<option value="技术">技术</option>
</select>
</td>
<!-- 单价 -->
<td class="price"><input type="number" value="0.00"></td>
<!-- 数量 -->
<td class="quantity"><input type="number" value="0"></td>
<!-- 总计 -->
<td class="total"><input type="number" value="0.00" disabled></td>
</tr>
<tr>
<td class="No">4</td>
<!-- 书名 -->
<td><input type="text" value=""></td>
<!-- 作者 -->
<td><input type="text" value=""></td>
<!-- 类别 -->
<td>
<select>
<option value="请选择类别..." disabled selected>请选择类别...</option>
<option value="商业">商业</option>
<option value="小说">小说</option>
<option value="数学">数学</option>
<option value="技术">技术</option>
</select>
</td>
<!-- 单价 -->
<td class="price"><input type="number" value="0.00"></td>
<!-- 数量 -->
<td class="quantity"><input type="number" value="0"></td>
<!-- 总计 -->
<td class="total"><input type="number" value="0.00" disabled></td>
</tr>
<tr>
<td class="No">5</td>
<!-- 书名 -->
<td><input type="text" value=""></td>
<!-- 作者 -->
<td><input type="text" value=""></td>
<!-- 类别 -->
<td>
<select>
<option value="请选择类别..." disabled selected>请选择类别...</option>
<option value="商业">商业</option>
<option value="小说">小说</option>
<option value="数学">数学</option>
<option value="技术">技术</option>
</select>
</td>
<!-- 单价 -->
<td class="price"><input type="number" value="0.00"></td>
<!-- 数量 -->
<td class="quantity"><input type="number" value="0"></td>
<details>
<summary>英文:</summary>
You can add an event listener on input of all your inputs elements:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
// Get all inputs
let inputs = document.querySelectorAll("input");
let grandTotal = 0;
inputs.forEach((input) => input.addEventListener('input', () => calculateTotal()))
function calculateTotal() {
let rows = document.querySelectorAll("tbody tr");
grandTotal = 0
let negative = false;
for (let i = 0; i < rows.length; i++) {
let row = rows[i];
let quantityInput = row.querySelector(".quantity input");
let priceInput = row.querySelector(".price input");
let totalInput = row.querySelector(".total input");
let quantity = parseInt(quantityInput.value);
let price = parseFloat(priceInput.value).toFixed(2);
if (quantity < 0 || price < 0) {
negative = true;
quantity = 0;
price = 0.00;
quantityInput.value = "0";
priceInput.value = "0.00";
}
let total = quantity * price;
totalInput.value = total.toFixed(2);
grandTotal += total;
}
if (negative) {
alert("No Negative Values!")
}
}
function updateGrandTotal () {
document.querySelector(".GrandTotal").value = grandTotal.toFixed(2);
}
<!-- language: lang-html -->
<h1>Book Ordering System</h1>
<table id="myTable">
<thead>
<tr>
<th>No.</th>
<th>Book Title</th>
<th>Author</th>
<th>Category</th>
<th>Unit Price</th>
<th>Quantity</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr>
<td class="No">1</td>
<!--Book Title-->
<td><input type="text" value=""></td>
<!--Author-->
<td><input type="text" value=""></td>
<!--Category-->
<td>
<select>
<option value="Please choose the category..." disabled selected>Please choose the category...</option>
<option value="Business">Business</option>
<option value="Fiction">Fiction</option>
<option value="Mathematics">Mathematics</option>
<option value="Technology">Technology</option>
</select>
</td>
<!--Unit Price-->
<td class="price"><input type="number" value="0.00"></td>
<!--Quantity-->
<td class="quantity"><input type="number" value="0"></td>
<!--Total-->
<td class="total"><input type="number" value="0.00" disabled></td>
</tr>
<tr>
<td class="No">2</td>
<!--Book Title-->
<td><input type="text" value=""></td>
<!--Author-->
<td><input type="text" value=""></td>
<!--Category-->
<td>
<select>
<option value="Please choose the category..." disabled selected>Please choose the category...</option>
<option value="Business">Business</option>
<option value="Fiction">Fiction</option>
<option value="Mathematics">Mathematics</option>
<option value="Technology">Technology</option>
</select>
</td>
<!--Unit Price-->
<td class="price"><input type="number" value="0.00"></td>
<!--Quantity-->
<td class="quantity"><input type="number" value="0"></td>
<!--Total-->
<td class="total"><input type="number" value="0.00" disabled></td>
</tr>
<tr>
<td class="No">3</td>
<!--Book Title-->
<td><input type="text" value=""></td>
<!--Author-->
<td><input type="text" value=""></td>
<!--Category-->
<td>
<select>
<option value="Please choose the category..." disabled selected>Please choose the category...</option>
<option value="Business">Business</option>
<option value="Fiction">Fiction</option>
<option value="Mathematics">Mathematics</option>
<option value="Technology">Technology</option>
</select>
</td>
<!--Unit Price-->
<td class="price"><input type="number" value="0.00"></td>
<!--Quantity-->
<td class="quantity"><input type="number" value="0"></td>
<!--Total-->
<td class="total"><input type="number" value="0.00" disabled></td>
</tr>
<tr>
<td class="No">4</td>
<!--Book Title-->
<td><input type="text" value=""></td>
<!--Author-->
<td><input type="text" value=""></td>
<!--Category-->
<td>
<select>
<option value="Please choose the category..." disabled selected>Please choose the category...</option>
<option value="Business">Business</option>
<option value="Fiction">Fiction</option>
<option value="Mathematics">Mathematics</option>
<option value="Technology">Technology</option>
</select>
</td>
<!--Unit Price-->
<td class="price"><input type="number" value="0.00"></td>
<!--Quantity-->
<td class="quantity"><input type="number" value="0"></td>
<!--Total-->
<td class="total"><input type="number" value="0.00" disabled></td>
</tr>
<tr>
<td class="No">5</td>
<!--Book Title-->
<td><input type="text" value=""></td>
<!--Author-->
<td><input type="text" value=""></td>
<!--Category-->
<td>
<select>
<option value="Please choose the category..." disabled selected>Please choose the category...</option>
<option value="Business">Business</option>
<option value="Fiction">Fiction</option>
<option value="Mathematics">Mathematics</option>
<option value="Technology">Technology</option>
</select>
</td>
<!--Unit Price-->
<td class="price"><input type="number" value="0.00"></td>
<!--Quantity-->
<td class="quantity"><input type="number" value="0"></td>
<!--Total-->
<td class="total"><input type="number" value="0.00" disabled></td>
</tr>
</tbody>
<tfoot>
<tr>
<td class="background-colour" colspan="5">
<button type="button" onclick="updateGrandTotal()" class="Button">Calculate Grand Total Price</button>
</td>
<td class="background-colour" colspan="2">
<input type="number" name="Grand Total Price" value="0.00" class="GrandTotal" disabled>
</td>
</tr>
</tfoot>
</table>
<!-- end snippet -->
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论