[英]Adding Input Value Together properly and showing it in and total input box
Am having problem adding my multiple input value together and showing it in a input value, i do get the input with id and i convert them to int with parseInt() but i do noticed that if i don't fill all input the total shows NAN and am evening having problem displaying the total value in the total_charge input.. 我有问题将我的多个输入值加在一起并在输入值中显示它,我确实得到id的输入,我用parseInt()将它们转换为int但我注意到如果我没有填写所有输入总显示NAN和晚上有问题显示total_charge输入中的总值..
i already tried what i can but can't seems to fixed it 我已经尝试过我可以但似乎无法修复它
My HTML code : 我的HTML代码:
<input type="text" id="consult_charge" placeholder="consult" onkeyup="addValue();"><br><br>
<input type="text" id="medicine_charge" placeholder="medicine" onkeyup="addValue();"><br><br>
<input type="text" id="injection_charge" placeholder="injection" onkeyup="addValue();"><br><br>
<input type="text" id="blood_report_charge" placeholder="blood report" onkeyup="addValue();"><br><br>
<input type="text" id="xray_charge" placeholder="xray" onkeyup="addValue();"><br><br>
<input type="text" id="total_charge" placeholder="total"><br><br>
My JS code : 我的JS代码:
function addValue(){
var consult = document.getElementById('consult_charge').value;
consult = parseInt(consult);
var medicine = document.getElementById('medicine_charge').value;
medicine = parseInt(medicine);
var injection = document.getElementById('injection_charge').value;
injection = parseInt(injection);
var blood = document.getElementById('blood_report_charge').value;
blood = parseInt(blood);
var xray = document.getElementById('xray_charge').value;
xray = parseInt(xray);
var total = document.getElementById('total_charge').value;
if (consult == '' && medicine != '' && injection != '' & blood != '' && xray != '' ) {
total = medicine + injection + blood + xray;
} else if(consult != '' && medicine == '' && injection != '' & blood != '' && xray != '' ) {
total = consult + injection + blood + xray;
} else if(consult != '' && medicine != '' && injection == '' & blood != '' && xray != '' ) {
total = consult + medicine + blood + xray;
} else if(consult != '' && medicine != '' && injection != '' & blood == '' && xray != '' ) {
total = consult + medicine + injection + xray;
} else if(consult != '' && medicine != '' && injection != '' & blood != '' && xray == '' ) {
total = consult + medicine + injection + blood;
} else if(consult != '' && medicine != '' && injection != '' & blood != '' && xray != '' ){
total = consult + medicine + injection + blood + xray;
} else{
total = '';
}
console.log(total);
}
Or is there another method i can achieve these? 还是有另一种方法可以实现这些目标吗?
The issue is if no value is entered in medicine field 问题是如果在医学领域没有输入任何价值
var medicine = document.getElementById('medicine_charge').value;
medicine = parseInt(medicine);
this will return medicine
to be null
; 这将使medicine
null
;
We can do something like this -: 我们可以这样做 - :
function addValue(){
var consult = parseInt(document.getElementById('consult_charge').value) || 0;
var medicine = parseInt(document.getElementById('medicine_charge').value) || 0;
var injection = parseInt(document.getElementById('injection_charge').value) || 0;
var blood = parseInt(document.getElementById('blood_report_charge').value) || 0;
var xray = parseInt(document.getElementById('xray_charge').value) || 0;
var total = parseInt(document.getElementById('total_charge').value) || 0;
total = (consult + medicine + injection + blood + xray) || '';
document.getElementById('total_charge').value = total;
console.log(total);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.