正确地将输入值加在一起并在总输入框中显示它

时间:2019-04-16 10:38:31

标签: javascript

在将多个输入值加在一起并显示为输入值时遇到问题,我确实获得了id的输入,并使用parseInt()将它们转换为int,但是我确实注意到,如果我不填写所有输入总计显示NAN,晚上显示在total_charge输入中显示总计值时出现问题。

我已经尝试了我可以解决的问题,但似乎无法解决

我的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>

我的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);


    }

还是我可以实现这些目标的另一种方法?

1 个答案:

答案 0 :(得分:0)

问题在于在医学字段中是否未输入任何值

var medicine = document.getElementById('medicine_charge').value;
medicine = parseInt(medicine);

这将返回medicinenull

我们可以做这样的事情-:

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);
        }