首页 > 解决方案 > 在 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 中并显示......但我不确定如何让它显示(并确保它正在计算)。

谢谢!

标签: javascriptcalculation

解决方案


尝试用 . 替换。正如您可能猜到的那样,它将 的内容设置document.getElementById("orderTotal").value.display为orderTotal。您的上一行甚至没有引用 orderTotal。Idk 如果它最终会起作用,但我知道我的版本会起作用。document.getElementById("orderTotal").innerHTML = orderTotal<p>= orderTotal


推荐阅读