Javascript 价格计算器问题
Javascript Price Calculator Issues
我已经尝试过几次创建价格计算器,但是价格永远不会显示在最后,因为它应该是。我仍然是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>
你的代码中有一些问题。
-
document.forms["quote"]
- 删除#
并设置name
属性而不是id
。 - 将
var selectedEventDuration
更改为var selectedDuration = theForm.elements["selectedDuration"];
- 从
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中的小问题,如下所示,它将对您有所帮助。
function getEventDuration() {
var EventDuration = 0;
var theForm = document.forms["quote"];
var selectedEventDuration = theForm.elements["selectedDuration"];
for (var i = 0; i < selectedEventDuration.length; i++) {
if (selectedEventDuration[i].checked) {
EventDuration = eventDurationArray[selectedEventDuration[i].value];
break;
}
}
return EventDuration;
}
//DIV - TOTAL PRICE TEST
function getTotals() {
var totalPrice = getEventDuration();
var totalPriceDIV = document.getElementById("totalPrice");
totalPriceDIV.innerHTML = "Total: $" + totalPrice;
}
信息 :- # 是 jquery 元素的指示符,所以当你只使用 javascript 时,它不是必需的。
打开代码示例的检查器,显示elements
未定义。
我建议使用 document.querySelector
来查找活动的单选按钮。
EventDuration = document.querySelector("input[name= selectedDuration]:checked").value
此外,getElementById
期望只有一个id,没有#
。同样,我倾向于坚持document.querySelector
,让事情变得简单。
用document.getElementById("totalPrice");
更改document.getElementById("#totalPrice");
相关文章:
- 在指令控制器中使用$attrs时出现问题
- 将PHP变量传递给jQuery时遇到问题
- Canvas Html5绘图应用程序,移动画布会导致重大问题
- GPA计算器数学问题
- 我的基本计算器代码有什么问题
- 简单的Javascript计算器,出了点问题
- Javascript 价格计算器问题(复选框)
- Javascript 价格计算器问题
- JavaScript 计算器问题
- Javascript计算器问题
- JavaScript计算器的问题
- jQuery计算器;减法问题
- Javascript计算器未回答的问题
- JavaScript贷款计算器问题
- 简单的JS计算器输出问题
- 自定义计算器的Javascript数学问题
- JavaScript计算器问题与清除按钮
- Javascript计算器-问题与逗号
- 捐赠日计算器问题
- 在internet explorer中连接google图表到javaScript计算器的问题