如何修复我的阵列/网页表单

时间:2014-12-10 08:50:02

标签: html arrays webforms

我目前正致力于一项有趣的编程任务。由于某种原因,“立即订购”按钮不起作用。所以我假设它与Array有关。 (公平的警告我对编程很新。)

到目前为止,这就是我的全部内容:

<html>
<script>
//global variables
var totalNumber = 0;   // for total number of Pizzas ordered
var pizzaPrice = 9.50;   // price of each pizza
var pizzaPriceGourmet = 15.50;  //price for gourmet pizzas
var pizzaDelivery = 5;  //price for pizza delivery
var orderTotalPrice = 0;   // total cost of order


var customername = prompt('Please enter your name.') //Function to take customers details, and to find out what order type it is
var ordertype = prompt('Is the order for delivery or pickup? - Note: Delivery will cost an extra $5.')
if (ordertype == 'pickup') {
 alert ("Place your order when you're ready.")
} else if (ordertype == 'delivery') {
 alert = prompt('Please enter your delivery address.')
 alert = prompt('Please enter your phone 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.meat-lovers.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

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



 alert("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("total number of pizzas ordered:" + totalNumber);
    }


    if (totalNumber >12) {          // Test for total amount of Pizzas ordered
    alert("There is a limit of 12 pizzas per order. Please cancel Order and re-order.");
    } else {
    alert ("Total number of pizzas ordered:" +totalNumber);  // Total number of pizzas Ordered
}

</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>

<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= "meat lovers" > Meat-Lovers 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> (Please note : Maximum 12 pizzas per Order ) </i>`
</body>
</html>

因此,如果有人知道我的数组/网页表单的错误,我们将不胜感激。

1 个答案:

答案 0 :(得分:0)

问题在于肉类爱好者的披萨。您正在使用&#34; document.form.meat-lovers.value&#34;这意味着输入的名称必须是肉食爱好者,并且它不匹配。 所以,如果你改为: pizza [5] = document.form.meatlovers.value
并更改输入:  Meat-Lovers披萨
它会很完美。我删除了连字符,因为它似乎不起作用。