如果数量更新,如何获取订单总额
订单输入表包含产品名称,价格和数量列:
<table id="order-products" class="mobileorder-table">
<colgroup>
<col style="width: 80%;">
<col style="width: 10%;">
<col style="width: 10%;">
</colgroup>
<tbody>
<tr>
<td>
Product1
</td>
<td>
<span class="mobileorder-price">0,98</span>
</td>
<td>
<input data-product="4750211645618" class="quantity" id="product_Soodkogus" name="product.Soodkogus"
type="number" min="0" max="999999" value=""
onblur="orderSumRefresh()" />
</td>
</tr>
</tbody>
</table>
Order total <p id="js-doksumma"></p>
如果更改数量,则应更新订单总价值。我尝试过
<script>
function parseFloatFormatted(txt) {
if (typeof txt !== 'string' || txt === null || txt === "") {
return 0
}
return parseFloat(txt.replace(',', '.').replace(' ', ''))
}
function orderSumRefresh() {
let totalAmount = 0
const table = document.getElementById("order-products")
table.rows.forEach((row) => {
//for (let i in table.rows) {
// const row = table.rows[i]
const hind = row.cells[1].querySelector(".mobileorder-price").value
const kogus = row.cells[2].querySelector(".quantity").value
const rowSum = Math.round(parseFloatFormatted(hind)* parseFloatFormatted(kogus) * 100) / 100
totalAmount += rowSum
});
var dok = document.getElementById("js-doksumma")
dok.innerText = totalAmount.toFixed(2)
}
</script>
,但有错误
如何正确实施?纯CSS,JavaScript或查询应该使用吗?
现代镀铬浏览器用于手机,ASP.NET 6 MVC Razor应用程序。
Order entry form contains product name, price and quantity columns:
<table id="order-products" class="mobileorder-table">
<colgroup>
<col style="width: 80%;">
<col style="width: 10%;">
<col style="width: 10%;">
</colgroup>
<tbody>
<tr>
<td>
Product1
</td>
<td>
<span class="mobileorder-price">0,98</span>
</td>
<td>
<input data-product="4750211645618" class="quantity" id="product_Soodkogus" name="product.Soodkogus"
type="number" min="0" max="999999" value=""
onblur="orderSumRefresh()" />
</td>
</tr>
</tbody>
</table>
Order total <p id="js-doksumma"></p>
If quantity is changed, order total value should updated. I tried
<script>
function parseFloatFormatted(txt) {
if (typeof txt !== 'string' || txt === null || txt === "") {
return 0
}
return parseFloat(txt.replace(',', '.').replace(' ', ''))
}
function orderSumRefresh() {
let totalAmount = 0
const table = document.getElementById("order-products")
table.rows.forEach((row) => {
//for (let i in table.rows) {
// const row = table.rows[i]
const hind = row.cells[1].querySelector(".mobileorder-price").value
const kogus = row.cells[2].querySelector(".quantity").value
const rowSum = Math.round(parseFloatFormatted(hind)* parseFloatFormatted(kogus) * 100) / 100
totalAmount += rowSum
});
var dok = document.getElementById("js-doksumma")
dok.innerText = totalAmount.toFixed(2)
}
</script>
but got error
How to properly implement this ? Should pure CSS, javascript or query used?
Modern Chrome browser is used in mobile phone, ASP.NET 6 MVC Razor application.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如尼克·弗(Nick Vu)所说的第一个问题是for循环,我更改为:
我在代码中发现了更多问题,例如,儿童词的索引是错误的,使用
您可以看到3个孩子,而您正在寻找中间一个(索引:1)
然后,要访问您需要使用
.value
属性的输入元素的数据:**************************** ***编辑*****************
在答案发生了变化时更改代码。
用于访问HTML元素的数据,请使用
.innerhtml
属性。我建议您使用
console.log()
并记录一些变量,以查看代码是否存在错误。As Nick Vu said a first problem is in the for loop and I changed to:
I find more problems in the code for example the index of the childNodes is wrong, using
you can see there are 3 child and you are searching for the middle one (index: 1)
Then for accessing the data of the input element you need to use the
.value
property like this:********************* EDIT *******************
Changing the code as the answer has changed.
For accessing the data of the html element use
.innerHTML
property.I suggest you to use the
console.log()
and log some variable to see if there is somethink wrong with the code.尼克是正确的。请记住,该表。行不是数组,而是HTMLCollection。您可以简单地解决该问题:
如果您想自己查看“长度”属性已迭代,打开Dev Tools,从“元素”选项卡中选择表格,然后在控制台中运行此片段:
您将请参阅最后的迭代打印“长度”,然后引发异常。
Nick is correct. Remember that table.rows is not an array but an HTMLCollection. You can fix the issue simply doing:
If you want to see for yourself that there is an "length" property being iterated over, open the dev tools, select the table from the elements tab, and run this snippet in the console:
You will see the last iteration print "length" and then throw an exception.
您的问题来自于此处
值将是
"0"
和"length"
(不是您期望的索引),因此在尝试访问时会抛出错误row.cells[0].childNodes
(row.cells
未定义)我建议您将其修改为
完整代码可以是
Your problem is from here
The value will be
"0"
and"length"
(not index like your expectation), so it throws an error while trying to accessrow.cells[0].childNodes
(row.cells
is undefined)I'd suggest you modify it to
The full code can be