3 回答
TA贡献1826条经验 获得超6个赞
这是修复,请注意带有FIX标签的评论
function processOrder() {
// FIX: you don't need this variable since it's not accessible from other functions
// orderTotal = 0;
sizeOfPizza();
// FIX: this function does not exist, put it back when you define it
// crustOfPizza();
//displayoutput();
var total = sizeOfPizza();
// FIX: use this approach
document.getElementById("orderTotal").innerText = total;
}
//display choice of size of pizza
function sizeOfPizza() {
// FIX: put the variable here so rest of this function can access it
let orderTotal = 0;
var size = document.getElementsByName("size");
if (size[0].checked) //if Personal is checked
{
orderTotal += 5.00 //add $5 to the total price
}
else if (size[1].checked) //if Medium is checked
{
orderTotal += 8.00 //add $8 to the total price
}
else if (size[2].checked) //if Large is checked
{
orderTotal += 10.00 //add $10 to the total price
}
else if (size[3].checked) //if Extra Large is checked
{
orderTotal += 12.00 //add $12 to the total price
}
else if (size[4].checked) //if Holy Pizza is checked
{
orderTotal += 20.00 //add $20 to the total price
}
// FIX: return the result
return orderTotal;
}
<h3>Select Size</h3>
<form action="" id="pizza-size">
<input type="radio" name="size" value="Personal" /> Personal (4 Slices) <br />
<input type="radio" name="size" value="Medium" /> Medium (8 slices)<br />
<input type="radio" name="size" value="Large" /> Large (10 slices) <br />
<input type="radio" name="size" value="Extra Large" /> Extra Large (12 slices)<br />
<input type="radio" name="size" value="Holy Pizza" /> Holy Pizza Batman (24 slices) <br />
</form>
<br />
<input type="button" onclick="processOrder()" value="Preview Order" />
<p>Total will appear here:</p>
<p id="orderTotal"></p>
TA贡献1801条经验 获得超16个赞
首先,您选择设置total
到sizeOfPizza()
哪个犯规返回任何值,因此total
为空。sizeOfPizza
和之间还有一个空格()
,可能会影响该函数的执行。您还应该将所有这些if else
语句更改为遍历数组size
或switch case
语句的迭代。您也不设置orderTotal
元素的值。要做到这一点就做document.getElementById("orderTotal").textContent = orderTotal;
试试这个,看看它是否有效。如果没有,请参考我的错误消息
TA贡献1836条经验 获得超13个赞
尝试替换document.getElementById("orderTotal").value.display
为document.getElementById("orderTotal").innerHTML = orderTotal
.This,正如您可能猜到的那样,将 的内容设置<p>
为 orderTotal。您的前一行甚至没有引用 orderTotal。我不知道它最后是否可以使用= orderTotal
,但我知道我的版本可以使用。
添加回答
举报