javascript - 在 JavaScript 中添加到总计
问题描述
我正在尝试使表单工作...我有基本代码,并且正在修改 orderTotal (最初设置为 0 )并根据用户单击的内容添加到其中。我正在寻找使用没有服务器的简单香草 Javascript。
function processOrder() {
orderTotal = 0;
sizeOfPizza();
crustOfPizza();
//displayoutput();
var total = sizeOfPizza();
document.getElementById("orderTotal").value.display
}
//display choice of size of pizza
function sizeOfPizza() {
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
}
}
<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>
因此,基本上,如果用户选择了中号披萨,我希望将 8.00 添加到 orderTotal 中并显示......但我不确定如何让它显示(并确保它正在计算)。
谢谢!
解决方案
尝试用 . 替换。正如您可能猜到的那样,它将 的内容设置document.getElementById("orderTotal").value.display
为orderTotal。您的上一行甚至没有引用 orderTotal。Idk 如果它最终会起作用,但我知道我的版本会起作用。document.getElementById("orderTotal").innerHTML = orderTotal
<p>
= orderTotal
推荐阅读
- oracle11g - Oracle to_date 返回不正确的结果
- r - 与二项式误差分布一起使用的链接函数
- r - R中具有所有内部颜色名称的颜色矩阵
- scala - akka-如何确保将动态数量的参与者的所有响应返回给父参与者?
- json - 如何使用 JQ 转换此 JSON 数据以依次将每个嵌套数组元素提取到顶层?
- javascript - 如何在不重新加载的情况下动态添加 React 组件?
- matlab - spap2的奇怪结果
- objective-c - 如何在 macOS 上将 MTLSharedTextureHandle 或 MTLSharedEventHandle 与 C XPC 接口一起使用?
- mongodb - 外部字段是数组时的MongoDB查找
- android - 我想做一个for循环,但它不会编译