我正在尝试查找用户输入的总价值。得到将数字与其相同值相乘的聚合
let Dem200 = document.querySelector(".input_200");
let output200 = document.querySelector(".result_200");
Dem200.addEventListener('blur', function() {
let result200 = parseInt(Dem200.value) * 200;
output200.innerHTML = result200;
})
let Dem100 = document.querySelector(".input_100");
let output100 = document.querySelector(".result_100");
Dem100.addEventListener('blur', function() {
let result100 = parseInt(Dem100.value) * 100;
output100.innerHTML = result100;
})
let Dem50 = document.querySelector(".input_50");
let Dem20 = document.querySelector(".input_20");
let Dem10 = document.querySelector(".input_10");
let Dem5 = document.querySelector(".input_5");
let Dem1 = document.querySelector(".input_1");
let output50 = document.querySelector(".result_50");
let output20 = document.querySelector(".result_20");
let output10 = document.querySelector(".result_10");
let output5 = document.querySelector(".result_5");
let output1 = document.querySelector(".result_1");
Dem50.addEventListener('blur', function() {
let result50 = parseInt(Dem50.value) * 50;
output50.innerHTML = result50;
})
Dem20.addEventListener('blur', function() {
let result20 = parseInt(Dem20.value) * 20;
output20.innerHTML = result20;
})
Dem10.addEventListener('blur', function() {
let result10 = parseInt(Dem10.value) * 10;
output10.innerHTML = result10;
})
Dem5.addEventListener('blur', function() {
let result5 = parseInt(Dem5.value) * 5;
output5.innerHTML = result5;
})
Dem1.addEventListener('blur', function() {
let result1 = parseInt(Dem1.value) * 1;
output1.innerHTML = result1;
})
window.onchange = function() {
var inputs = document.getElementsByTagName('input'),
resultinchancge = document.getElementById('total'),
sum = 0;
for (var i = 0; i < inputs.length; i++) {
var ip = inputs[i];
if (ip.name && ip.name.indexOf("total") < 0) {
sum += parseInt(ip.value) || 0;
}
}
resultinchancge.value = sum;
}
| 200 | ||
|---|---|---|
| 100 | ||
| 50 | ||
| 20 | ||
| 10 | ||
| 5 | ||
| 1 | ||
| Total1 |
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
请尝试这样做。这对你有用。
window.onchange = function() { var inputs = document.getElementsByTagName('input'); resultinchancge = document.getElementById('total'); const sum = [...document.querySelectorAll('[class*=result]')].reduce((r, e) => { return r + parseInt(e.textContent) }, 0) console.log(sum) resultinchancge.value = sum; }