在这里的一些用户的帮助下堆栈溢出我设法让我的脚本工作,但现在我收到了错误的输出。
关于它应该做什么的简要说明:代码应该接受汉堡的订单并显示订单。它将面包,肉和附加物等字符串作为字符串,然后在文本框中输出。
当我尝试输入例如: 输入:全麦 - >牛肉 - >奶酪产量:全麦 - >牛肉 - >奶酪= 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>
答案 0 :(得分:1)
我看到的代码之一更奇怪的修复... 你在“肉”功能中检查“面包”而不是“肉”。
修正:
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;
}