我的警报出了什么问题? (它根本不会运行

时间:2014-12-12 23:44:30

标签: javascript html arrays alert

对于2个警报(它们是相同的)“警报(”你订购的比萨饼是:“+ totalPizzaOrder)”它们实际上没有正常运行。一切都运行良好,直到我收到警报,然后他们只是没有出现。 (它们与数组相关联,所以我认为它可能与此有关。

那么,有谁知道我做错了什么?我现在已经盯着这么多个小时了。

<html>
<script>

 //global variables
 totalNumber = 0;   // for total number of Pizzas ordered
 pizzaPrice = 9.50;   // price of each pizza
 pizzaPriceGourmet = 15.50;  //price for gourmet pizzas
 orderTotalPrice = 0;   // total cost of order
 pizzaDeliveryPrice = 5; // price for pizza delivery
 var customerName = prompt("Please enter your name.") //prompt to get the customers name
 var customerNumber = prompt("Please enter your phone number.") //prompt to get the customers number 

function order()
{
  var pizza = new Array()
  pizza[0] = document.form.hawaiian.value                //allocates type of pizza in array
  pizza[0] = Number(pizza[0])                           //converts to number value
  pizza[1] = document.form.cheese.value                 //allocates type of pizza in array            
  pizza[1] = Number(pizza[1])                            //converts to number value
  pizza[2] = document.form.veggie.value                //allocates type of pizza in array
  pizza[2] = Number(pizza[2])                            //converts to number value
  pizza[3] = document.form.supreme.value                //allocates type of pizza in array
  pizza[3] = Number(pizza[3])                            //converts to number value
  pizza[4] = document.form.pepperoni.value                //allocates type of pizza in array
  pizza[4] = Number(pizza[4])                            //converts to number value
  pizza[5] = document.form.meatlovers.value                //allocates type of pizza in array
  pizza[5] = Number(pizza[5])                            //converts to number value
  pizza[6] = document.form.chicken.value                //allocates type of pizza in array
  pizza[6] = Number(pizza[6])                            //converts to number value
  pizza[7] = document.form.prawn.value                //allocates type of pizza in array
  pizza[7] = Number(pizza[7])                            //converts to number value

  totalPlain = pizza[0] + pizza[1] + pizza[2] + pizza[3] + pizza[4];
  totalGourmet = pizza[5] + pizza[6] + pizza[7];

totalNumber = totalGourmet + totalPlain

var totalPizzaOrder = alert("You have ordered: " + "\n" +        <----- I NEED THIS
         "Hawaiian Pizza: " + pizza [0] + "\n" +
         "Cheese Pizza: " + pizza [1] + "\n" +
         "Veggie Pizza: " + pizza [2] + "\n" + 
         "Supreme Pizza: " + pizza [3] + "\n" +
         "Pepperoni Pizza: " + pizza [4] + "\n" +
         "Meat-Lovers Pizza: " + pizza [5] + "\n" +
         "Chicken Pizza: " + pizza [6] + "\n" +
         "Prawn Pizza: " + pizza [7]);  


if (totalNumber >12) {          // Limit for total amount of Pizzas ordered
    alert("There is a limit of 12 pizzas per order. - PRESS 'Prevent this page from creating additional dialogs' THEN PRESS 'cancel order' AND THEN RE-ORDER! - Current total is: " +totalNumber);
    } else 
    alert("Total number of pizzas ordered: " + totalNumber); //Total amount of pizzas ordered
calculate()  //Begins calculation function
}

function calculate()    //Function for the cost of the order
{
    orderTotalPrice = (totalPlain*pizzaPrice + totalGourmet*pizzaPriceGourmet);  //order total+ amount of pizzas ordered * pizza price
    var pizzaDelivery = prompt('Would you like your order to be delivered for $5, or for pick up?    -Type in "1" for delivery, and "0" for pickup.') //asks if you want your order to be delivered or not
    orderTotalPrice = (orderTotalPrice + (pizzaDelivery*pizzaDeliveryPrice)); // calculates the total cost with or without the delivery cost
    alert("Total cost of pizzas is: $" + orderTotalPrice.toFixed(2) );  //Display order cost as "$"0.00
    if (pizzaDelivery == '1'){
    var response = prompt("Please enter your address: ")
    alert("The pizza should be delivered within the next 25 minutes, to this address:   " +response)
     alert("Thank you for ordering with Pete's Pizzas " +customerName)
     alert("If anything happens to go wrong we will contact you on your number: " +customerNumber)
     alert("The pizzas you ordered are: " +totalPizzaOrder)                   <----TO BE HERE
     alert("To exit, just click 'ok' and then close down the program!")
} else if (pizzaDelivery == '0'){
    alert("Your order will be ready for pickup in 15 minutes!")
     alert("Thank you for ordering with Pete's Pizzas " +customerName)
     alert("If anything happens to go wrong we will contact you on your number: " +customerNumber)
     alert("The pizzas you ordered are: " +totalPizzaOrder)                  <-------AND HERE
     alert("To exit, just click 'ok' and then close down the program!")
}
}


</script>

<body>
<h1> Welcome to Pete's Pizza! </h1>
<p> Please follow the prompts to place your order. </p>
<p> Menu: ($9.50) Hawaiian, Cheese, Veggie, supreme, pepperoni.</P>
<p> ($15.50) meat-lovers, chicken, prawn. </p>
<p> Please be aware that there is a $5 charge for deliveries. </p>
<p> (Please note : Maximum 12 pizzas per Order ) </p>

<form name ="form">
<input type="text" name= "hawaiian" > Hawaiian Pizza <br>
<input type="text" name= "cheese" > Cheese Pizza <br>
<input type="text" name= "veggie" > Veggie Pizza <br>
<input type="text" name= "supreme" > Supreme Pizza <br>
<input type="text" name= "pepperoni" > Pepperoni Pizza <br>
<input type="text" name= "meatlovers" > MeatLovers Pizza <br>
<input type="text" name= "chicken" > Chicken Pizza <br>
<input type="text" name= "prawn" > Prawn Pizza <br>

<input type="button" value="order now" onClick="order()">
<input type="button" value="cancel order" onClick="window.location.reload()">
</form>



<i> If you need to cancel the order at anytime press the 'Prevent this page from creating     additional dialogs' button, then press the "cancel order" button. (By doing this, it will take you back to the begging of the program.)  </i>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您将totalPizzaOrder设置为警报的返回值,该值始终未定义。

相反,您应该将字符串存储在变量中,然后将alert存储在:

var totalPizzaOrder = "You have ordered: " + "\n" +
         "Hawaiian Pizza: " + pizza [0] + "\n" +
         "Cheese Pizza: " + pizza [1] + "\n" +
         "Veggie Pizza: " + pizza [2] + "\n" + 
         "Supreme Pizza: " + pizza [3] + "\n" +
         "Pepperoni Pizza: " + pizza [4] + "\n" +
         "Meat-Lovers Pizza: " + pizza [5] + "\n" +
         "Chicken Pizza: " + pizza [6] + "\n" +
         "Prawn Pizza: " + pizza [7];

alert(totalPizzaOrder);

现在totalPizzaOrder设置正确。

您还应该使用全局变量在totalPizzaOrder之外声明order(),或将其作为参数传递到calculate()以确保其可用。