Javascript成本计算器不会像所谓的那样工作

时间:2017-11-26 13:43:35

标签: javascript html forms function

您好我是Js的新手,我想制作成本计算功能。到目前为止,它的工作原理,但它不是我想要的。这是它的外观

<script>
                    function finalCost(){
                    var roomType = document.getElementById("roomtype").value;
                    var roomNum = document.getElementById("rooms").value;
                    var personNum = document.getElementById("atoma").value;
                    var childNum = document.getElementById("paidia").value;
                    var resFacilities = 
                    document.getElementById("meal").value;
                    var atoma = childNum + personNum;
                    var roomty = (parseInt(roomType));                                              

                    var total = +roomty + +atoma + +((resFacilities));


                    document.getElementById("result").innerHTML = total;
                    }
                    </script>

但是,我希望有类似的东西:(我知道它不会工作)

<script>
                    function finalCost(){
                    var roomType = document.getElementById("roomtype").value;
                    var roomNum = document.getElementById("rooms").value;
                    var personNum = document.getElementById("atoma").value;
                    var childNum = document.getElementById("paidia").value;
                    var resFacilities = 
                    document.getElementById("meal").value;
                    var atoma = childNum + personNum;
                    var roomty = (parseInt(roomType));


                    var total = +roomty*roomNum  + +atoma + +((resFacilities)*atoma);


                    document.getElementById("result").innerHTML = total;
                    }
                    </script>

如果我输入上面的代码,那么费用根本不会工作如果我在没有* roomNum的情况下输入它,那么费用将在没有包含餐费的情况下生效。

请给我一些建议,因为我对此问题感到非常沮丧。

1 个答案:

答案 0 :(得分:0)

如果我理解正确,因为我不确定您要计算的确切计算:

    // Total calculated as room type times number of rooms times number of people. 
    // Adding meal costs per person.
    var total = roomty*roomNum*atoma +(resFacilities*atoma);