使用javascript根据html表单中的产品数量计算订单的总成本

时间:2017-07-27 04:12:54

标签: javascript html forms

尝试使用javascript计算订单的总成本,使用通过html中的表单输入的数量,但总数未显示在输入框中。一直在乱搞它几天,昨天显示NaN,但现在盒子保持完全空白。这些都在单个网页中作为学校的实际评估,我只是使用脚本标记。

见下面的js

function calculatePrice()
            {
            //select data  
            var cappuccino = 3.00;
            var espresso = 2.25;
            var latte = 2.50;
            var iced = 2.50;

            var quantityCappuccino = document.getElementByID("quantityCappuccino").value;
            var quantityEspresso = document.getElementByID("quantityEspresso").value;
            var quantityLatte = document.getElementByID("quantityLatte").value;
            var quantityIced = document.getElementByID("quantityIced").value;

            //calculate final cost  
            var total = (quantityCappuccino * cappuccino) + (quantityEspresso * espresso) + (quantityLatte * latte) + (quantityIced * iced); 

            //print value to orderTotal 
            document.getElementById("orderTotal").value=total;
        }

这是表单的html

 <table>
                    <tr align="center"> 
                        <td><hr>
                            Hot Drinks<hr>                  
                        </td>
                        <td><hr>
                            Price<hr>
                        </td>
                        <td><hr>
                            Quantity<hr>
                        </td>
                    </tr>
                <form name="calcuccino">
                    <tr>
                        <td>
                            Cappuccino  
                        </td>
                        <td align="center">
                            $3.00
                        </td>
                        <td align="center">
                            <input type="number" id="quantityCappucino" name="quantityCappuccino" value="0">
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Espresso
                        </td>
                        <td align="center">
                            $2.25
                        </td>
                        <td align="center">
                            <input type="number" id="quantityEspresso" name="quantityEspresso" value="0">
                        </td>
                    </tr>
                    <tr>
                        <td>
                                Latte
                        </td>
                        <td align="center">
                            $2.50
                        </td>
                        <td align="center">
                            <input type="number" id="quantityLatte" name="quantityLatte" value="0">
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Iced
                        </td>
                        <td align="center">
                            $2.50
                        </td>
                        <td align="center">
                            <input type="number" id="quantityIced" name="quantityIced" value="0">
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <hr>
                            <input type="checkbox" id="takeaway" name="takeaway">Takeaway?</option>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <br>
                            <button type="button" onclick="calculatePrice()">Submit Order</button>
                        </td>
                        <td>
                        </td>
                        <td>
                            <br>
                            <hr>
                            Order total: <b>$</b>
                            <input type="text"  name="orderTotal" id="orderTotal" Size=6 readonly>

2 个答案:

答案 0 :(得分:0)

我发现了两个错误:

(1)在接下来的四个var语句中,getElementById而不是getElementByID(不要全押“Id”)。

(2)您的第一个<input>标记的ID名称拼写错误。它应该是quantityCappuccino(与name属性相同)。

修好后,代码就像魅力一样。

注意:我花了几秒钟才弄明白,因为错误控制台(在FireFox中,按F12键打开它)报告了问题。那个控制台是你的朋友。

答案 1 :(得分:0)

试试这个!

&#13;
&#13;
$("#orderTotal").click(function () {
    calculatePrice();
});

function calculatePrice()
{
    //select data
    var cappuccino = 3.00;
    var espresso = 2.25;
    var latte = 2.50;
    var iced = 2.50;

    var quantityCappuccino = $("#quantityCappucino").val();
    var quantityEspresso =  $("#quantityEspresso").val();
    var quantityLatte =  $("#quantityLatte").val();
    var quantityIced = $("#quantityIced").val();

    //calculate final cost
    var total = (quantityCappuccino * cappuccino) + (quantityEspresso * espresso) + (quantityLatte * latte) + (quantityIced * iced);

    console.log(total);
    //print value to orderTotal
    $("#orderTotal").val(total);
}
&#13;
<!DOCTYPE html>
<html>
<head>
    <title>Title of the document</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
   
</head>

<body>
<table>
    <tr align="center">
        <td><hr>
            Hot Drinks<hr>
        </td>
        <td><hr>
            Price<hr>
        </td>
        <td><hr>
            Quantity<hr>
        </td>
    </tr>
    <form name="calcuccino">
        <tr>
            <td>
                Cappuccino
            </td>
            <td align="center">
                $3.00
            </td>
            <td align="center">
                <input type="number" id="quantityCappucino" name="quantityCappuccino" value="0">
            </td>
        </tr>
        <tr>
            <td>
                Espresso
            </td>
            <td align="center">
                $2.25
            </td>
            <td align="center">
                <input type="number" id="quantityEspresso" name="quantityEspresso" value="0">
            </td>
        </tr>
        <tr>
            <td>
                Latte
            </td>
            <td align="center">
                $2.50
            </td>
            <td align="center">
                <input type="number" id="quantityLatte" name="quantityLatte" value="0">
            </td>
        </tr>
        <tr>
            <td>
                Iced
            </td>
            <td align="center">
                $2.50
            </td>
            <td align="center">
                <input type="number" id="quantityIced" name="quantityIced" value="0">
            </td>
        </tr>
        <tr>
            <td>
                <hr>
                <input type="checkbox" id="takeaway" name="takeaway">Takeaway?</option>
            </td>
        </tr>
        <tr>
            <td>
                <br>
                <button type="button" onclick="calculatePrice()">Submit Order</button>
            </td>
            <td>
            </td>
            <td>
                <br>
                <hr>
                Order total: <b>$</b>
                <input type="text"  name="orderTotal" id="orderTotal" Size=6 readonly>
            </td>
        </tr>
        </form>
</table>
</body>
</html>
&#13;
&#13;
&#13;