繁体   English   中英

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

[英]What is wrong with my alert? (it won't run at all

对于2个警报(它们是相同的)“ alert(“您订购的披萨是:” + 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>

您将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设置为正确的值。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM