gpt4 book ai didi

javascript - Jquery 乘法不起作用?但加法有效

转载 作者:行者123 更新时间:2023-11-28 13:03:02 25 4
gpt4 key购买 nike

我正在尝试将输入框中的 2 个值相乘并将其存储在该列的最后一行中。 加法非常适合,但乘法不起作用。

我想将所有行输入相乘。我有一个加法示例,为什么乘法不起作用?

我已经尝试过
tot *= Number($(this).val()) || 0//这不起作用。
tot = ((tot) * (Number($(this).val()) || 0))//这不起作用。

$('table input').on('input', function() {
var $tr = $(this).closest('tr'); // get tr which contains the input
var tot = 0; // variable to sore sum
$('input', $tr).each(function() { // iterate over inputs
tot += Number($(this).val()) || 0 // i want to multiply here. The addition works perfectly
});
$('td:last', $tr).text(tot); // update last column value
}).trigger('input'); // trigger input to set initial value in columns

最佳答案

您也可以尝试此代码片段。这里我指定了 tot = 1,因为与零相乘将提供零结果。所以初始化一定不能为零,我将乘法语句修改为

"tot *= Number($(this).val())"

最终代码为:

$('table input').on('input', function() {
var $tr = $(this).closest('tr');
var tot = 1; //variable to store product
$('input', $tr).each(function() {
tot *= Number($(this).val())
});
$('td:last', $tr).text(tot);
}).trigger('input');

关于javascript - Jquery 乘法不起作用?但加法有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48982935/

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