通过增加一个值,另一个值也增加该数量
By increasing a value, another value is increased by that amount
所以我有这段代码,我想用一个按钮增加数量,随着数量的增加,重量也会随之改变。
下面代码的结果,通过点击按钮innerText变为
例如
1 件 = 8 克
2 件 = 16 克
3piece = 48 g -> 这就是问题所在 - 我希望数字增加第一个值 (3 * 8 = 24),但它正在做 (3 * 16)
乘法时如何设置权重变量固定?权重不是设置为 8 的变量,而是对每个元素都有不同的值,这就是为什么它在 const 中调用的原因。元素来自数组 map
中的 JSON 文件
let amount = document.getElementById('amount');
let weight = document.getElementById('weight');
function add(am, wg) {
{
amount = parseInt(document.getElementById(am).innerText);
weight= parseInt(document.getElementById(wg).innerText);
amount++
let totalPt = weight * amount
document.getElementById(am).innerText = amount;
document.getElementById(wg).innerText = totalPt;
}
}
您需要知道每个重量的数量(只需 weight / amount
),然后您可以将其乘以增加的数量
function add(am, wg) {
amount = parseInt(document.getElementById(am).innerText);
weight = parseInt(document.getElementById(wg).innerText);
baseweight = weight / amount;
amount++;
let totalPt = baseweight * amount;
document.getElementById(am).innerText = amount;
document.getElementById(wg).innerText = totalPt;
}
如果您希望权重在乘法时固定为 8,则只需将数量设置为另一个变量并在乘法运算中使用该变量即可。
在下面的代码中,我使用了 fixedWeight 来存储每个单位的固定重量。您可以在乘法时使用此变量。
function add(am, wg) {
{
amount = parseInt(document.getElementById(am).innerText);
weight= parseInt(document.getElementById(wg).innerText);
const fixedWeight = 8;
amount++
let totalPt = fixedWeight * amount
document.getElementById(am).innerText = amount;
document.getElementById(wg).innerText = totalPt;
}
}
所以我有这段代码,我想用一个按钮增加数量,随着数量的增加,重量也会随之改变。
下面代码的结果,通过点击按钮innerText变为 例如
1 件 = 8 克
2 件 = 16 克
3piece = 48 g -> 这就是问题所在 - 我希望数字增加第一个值 (3 * 8 = 24),但它正在做 (3 * 16)
乘法时如何设置权重变量固定?权重不是设置为 8 的变量,而是对每个元素都有不同的值,这就是为什么它在 const 中调用的原因。元素来自数组 map
中的 JSON 文件let amount = document.getElementById('amount');
let weight = document.getElementById('weight');
function add(am, wg) {
{
amount = parseInt(document.getElementById(am).innerText);
weight= parseInt(document.getElementById(wg).innerText);
amount++
let totalPt = weight * amount
document.getElementById(am).innerText = amount;
document.getElementById(wg).innerText = totalPt;
}
}
您需要知道每个重量的数量(只需 weight / amount
),然后您可以将其乘以增加的数量
function add(am, wg) {
amount = parseInt(document.getElementById(am).innerText);
weight = parseInt(document.getElementById(wg).innerText);
baseweight = weight / amount;
amount++;
let totalPt = baseweight * amount;
document.getElementById(am).innerText = amount;
document.getElementById(wg).innerText = totalPt;
}
如果您希望权重在乘法时固定为 8,则只需将数量设置为另一个变量并在乘法运算中使用该变量即可。
在下面的代码中,我使用了 fixedWeight 来存储每个单位的固定重量。您可以在乘法时使用此变量。
function add(am, wg) {
{
amount = parseInt(document.getElementById(am).innerText);
weight= parseInt(document.getElementById(wg).innerText);
const fixedWeight = 8;
amount++
let totalPt = fixedWeight * amount
document.getElementById(am).innerText = amount;
document.getElementById(wg).innerText = totalPt;
}
}