JavaScript-我从脚本中得到错误的结果

JavaScript-我从脚本中得到错误的结果,javascript,html,Javascript,Html,在stack overflow上的一些用户的帮助下,我成功地让脚本工作,但现在我只收到了错误的输出 关于它应该做什么的简要说明:代码应该接受汉堡的订单并显示订单。它将面包、肉和其他物品作为字符串输入,然后在文本框中输出 当我尝试输入时,例如: 输入:全麦->牛肉->奶酪输出:全麦->牛肉->奶酪=4.50英镑 但对于: 输入:白色->鹿肉->奶酪输出:白色->鸡肉->奶酪-4英镑 产出的鹿肉换成了鸡肉,这显然是错误的。有什么帮助吗 <html> <head> &

在stack overflow上的一些用户的帮助下,我成功地让脚本工作,但现在我只收到了错误的输出

关于它应该做什么的简要说明:代码应该接受汉堡的订单并显示订单。它将面包、肉和其他物品作为字符串输入,然后在文本框中输出

当我尝试输入时,例如: 输入:全麦->牛肉->奶酪输出:全麦->牛肉->奶酪=4.50英镑

但对于: 输入:白色->鹿肉->奶酪输出:白色->鸡肉->奶酪-4英镑 产出的鹿肉换成了鸡肉,这显然是错误的。有什么帮助吗

<html>

<head>
    <style>
        body {
            background-color: #d0e4fe;
        }
        p1 {
            color: orange;
        }
        p1 {
            font-family: "Times New Roman";
            font-size: 20px;
        }
        p2 {
            color: white;
        }
        p2 {
            font-family: "Times New Roman";
            font-size: 15px;
        }
        p3 {
            color: orange;
        }
        p3 {
            font-family: "Times New Roman";
            font-size: 20px;

        }

        p.normal {font-weight:normal;}
        p.light {font-weight:lighter;}
        p.thick {font-weight:bold;}
        p.thicker {font-weight:900;}
    </style>
    <script>
        // global variables
        var price = 0.0;
        var string;

        function order() {
            // function to call other functions
            price = 0.0;
            string = "";
            string = bread(string);
            string = meat(string);
            string = extras(string);
            string = cost(string);
            output(string);
        }

        function bread(string) {
            // check which radio button is selected
            // checkboxes – an array of controls        
            if (document.form1.bread[0].checked) {
                string = "Wholemeal Bap";
                price = price + 1.00;
            }
            if (document.form1.bread[1].checked) {
                string = "White Bap";
                price = price + 1.00;
            }
            if (document.form1.bread[2].checked) {
                string = "Sesame Bap";
                price = price + 1.00;
            }

            return string;
        }

        function meat(string) {
            // check which radio button is selected
            // checkboxes – an array of controls        
            if (document.form1.meat[0].checked) {
                string = string + "," + document.form1.meat[0].value;
                price = price + 3.00;
            }
            if (document.form1.bread[1].checked) {
                string = string + "," + document.form1.meat[1].value;
                price = price + 2.50;
            }
            if (document.form1.bread[2].checked) {
                string = string + "," + document.form1.meat[2].value;
                price = price + 3.50;
            }

            return string;
        }

        function extras(string) {
            // using a loop to check which checkboxes have been checked
            // updating the string to show the extras choices
            // updating the price accordingly
            for (var i = 0; i < document.form1.extras.length; i++) {
                if (document.form1.extras[i].checked) {
                    string = string + "," + document.form1.extras[i].value;
                    switch (i) {
                    case 0:
                        price = price + 0.50;
                        break;
                    case 1:
                        price = price + 0.50;
                        break;
                    case 2:
                        price = price + 0.50;
                        break;
                    case 3:
                        price = price + 0.50;
                        break;
                    case 4:
                        price = price + 0.50;
                        break;
                    case 5:
                        price = price + 0.50;
                        break;

                    }
                }

            }
            return string;
        }

        function cost(string) {
            string = string + " = £" + price;
            return string;
        }

        function output() {
            document.form1.receipt.value = string;
        }
    </script>
</head>

<body>
    </br>
    <br/>
    <br/>
    <form name="form1">
        <p1 class="thicker">Select your Bread Base:</p1><br>
        <p2>Wholemeal = £1</p2><br>
        <p2>White = £1</p2><br>
        <p2>Sesame = £1</p2>
        <p>
            <input type="radio" name="bread" value="Wholemeal">Wholemeal
            <br>
            <input type="radio" name="bread" value="White">White
            <br>
            <input type="radio" name="bread" value="Sesame">Sesame
        </p>
        <p1 class="thicker">Select your Meat Base:</p1><br>
        <p2>Beef = £3</p2><br>
        <p2>Chicken = £2.50</p2><br>
        <p2>Venison - £3.50</p2>
        <p>
            <input type="radio" name="meat" value="Beef">Beef
            <br>
            <input type="radio" name="meat" value="Chicken">Chicken
            <br>
            <input type="radio" name="meat" value="Venison">Venison
        </p>
        <p1 class="thicker">Select any toppings - £0.50 each</p1>

        <p>
            <input type="checkbox" name="extras" value="Cheese">Cheese
            <input type="checkbox" name="extras" value="Mushrooms">Mushrooms
            <input type="checkbox" name="extras" value="Lettuce">Lettuce
            <br>
            <input type="checkbox" name="extras" value="Tomato">Tomato
            <input type="checkbox" name="extras" value="Onions">Onions
            <input type="checkbox" name="extras" value="Relish">Relish
        </p>
        <textarea name="receipt" rows="5" cols="50"></textarea>
        </br>
        <input type="button" value="Add Burger!" onclick="order()">
    </form>

</body>

</html>

我见过的一个更奇怪的修复我的代码。。。 在你的肉食功能中,你检查的是面包而不是肉

固定的:

    function meat(string) {
        // check which radio button is selected
        // checkboxes – an array of controls        
        if (document.form1.meat[0].checked) {
            string = string + "," + document.form1.meat[0].value;
            price = price + 3.00;
        }
        if (document.form1.meat[1].checked) {
            string = string + "," + document.form1.meat[1].value;
            price = price + 2.50;
        }
        if (document.form1.meat[2].checked) {
            string = string + "," + document.form1.meat[2].value;
            price = price + 3.50;
        }

        return string;
    }

我相信这个错误在函数中的某个地方,但我无法找出哪里出了问题。看起来像是复制/粘贴问题。在初始输入之后,您正在检查肉函数中的面包输入。该死的,我没看见。谢谢你回答了我的问题。