我的 javascript 代码不起作用,它应该计算午餐菜单项的总价格并给出总结果,但没有任何结果。html 工作正常,所以我只是在这里写 javascript 部分,它很短。我确实按照指示获取此代码,所以我不明白出了什么问题。谢谢 :)
function calcTotal()
{
var itemTotal=0;
var items=document.getElementsByTagName("input");
//collects them into a NodeList object, which has indices
for(var i=0;i<5;i++)
{
if(items[i].checked)
itemTotal+=(items[i].value*1); //the *1 turns it into a number
}
document.getElementById("total").innerHTML="Your order total is $"+itemTotal + ".00";
var submitButton = document.getElementById("sButton");
if(submitButton.addEventListener)
{
submitButton.addEventListener("click",calcTotal,false);
}
else if(submitButton.attachEvent)
{
submitButton.attachEvent("onclick", calcTotal);
}
}