gpt4 book ai didi

JavaScript 价格计算器问题

转载 作者:行者123 更新时间:2023-11-28 15:12:59 24 4
gpt4 key购买 nike

我曾多次尝试创建价格计算器,但是价格从未像应该的那样显示在最后。我对 Javascript 还很陌生,所以我不确定是否有代码错误。我相信我足够理解它应该出现,但也许这里有人可能会看到我在代码中做错了什么?

var eventDurationArray = new Array();
eventDurationArray["2hrs"]=120;
eventDurationArray["3hrs"]=180;
eventDurationArray["4hrs"]=240;
eventDurationArray["5hrs"]=300;
eventDurationArray["6hrs"]=360;

//RADIO BUTTON - EVENT DURATION TEST
function getEventDuration() {
var EventDuration = 0;

var theForm = document.forms["#quote"];

var selectedEventDuration = theForm.elements["selectedDuration"];

for(var i = 0; i < selectedDuration.length; i++)
{
if(selectedDuration[i].checked)
{
EventDuration = eventDurationArray[selectedDuration[i].value];

break;
}
}

return EventDuration;
}

//DIV - TOTAL PRICE TEST
function getTotals() {
var totalPrice = getEventDuration();
var totalPriceDIV = document.getElementById("#totalPrice");
totalPriceDIV.innerHTML = "Total: $"+totalPrice;
}
<form id="quote" action="" onsubmit="return false;">
<input type="radio" name="selectedDuration" value="2hrs" onclick="getTotals()" />
Round cake 6" - serves 8 people ($20)
<input type="radio" name="selectedDuration" value="3hrs" onclick="getTotals()" />
Round cake 8" - serves 12 people ($25)
<input type="radio" name="selectedDuration" value="4hrs" onclick="getTotals()" />
Round cake 10" - serves 16 people($35)
<input type="radio" name="selectedDuration" value="5hrs" onclick="getTotals()" />
Round cake 12" - serves 30 people($75)
<br />
<br />
<div id="totalPrice" style="color: red; text-align: center; font-size: 18px;"></div>
</form>

最佳答案

您的代码存在一些问题..

  1. document.forms["quote"] - 删除 # 并设置 name 属性而不是 id .
  2. var selectedEventDuration 更改为 var selectedDuration = theForm.elements["selectedDuration"];
  3. nt.getElementById("#totalPrice"); 中删除 #

var eventDurationArray = new Array();
eventDurationArray["2hrs"]=120;
eventDurationArray["3hrs"]=180;
eventDurationArray["4hrs"]=240;
eventDurationArray["5hrs"]=300;
eventDurationArray["6hrs"]=360;

//RADIO BUTTON - EVENT DURATION TEST
function getEventDuration() {
var EventDuration = 0;

var theForm = document.forms["quote"];

var selectedDuration = theForm.elements["selectedDuration"];

for(var i = 0; i < selectedDuration.length; i++)
{
if(selectedDuration[i].checked)
{
EventDuration = eventDurationArray[selectedDuration[i].value];

break;
}
}

return EventDuration;
}

//DIV - TOTAL PRICE TEST
function getTotals() {
var totalPrice = getEventDuration();
var totalPriceDIV = document.getElementById("totalPrice");
totalPriceDIV.innerHTML = "Total: $"+totalPrice;
}
<form name="quote" action="" onsubmit="return false;">
<input type="radio" name="selectedDuration" value="2hrs" onclick="getTotals()" />
Round cake 6" - serves 8 people ($20)
<input type="radio" name="selectedDuration" value="3hrs" onclick="getTotals()" />
Round cake 8" - serves 12 people ($25)
<input type="radio" name="selectedDuration" value="4hrs" onclick="getTotals()" />
Round cake 10" - serves 16 people($35)
<input type="radio" name="selectedDuration" value="5hrs" onclick="getTotals()" />
Round cake 12" - serves 30 people($75)
<br />
<br />
<div id="totalPrice" style="color: red; text-align: center; font-size: 18px;"></div>
</form>

关于JavaScript 价格计算器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35432070/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com