简体   繁体   English

Javascript价格计算器的问题-价格未显示

[英]Issues with Javascript Price Calculator - Price Not Showing

I'm having issues with a simple price calculator I'm making. 我在制作一个简单的价格计算器时遇到问题。 I've learned Javascript and I went over everything and it looks fine, but I'm definitely missing something since the final price isn't appearing. 我已经学习过Javascript,并且遍历了所有内容,而且看起来还不错,但是由于最终价格没有出现,我肯定会缺少一些东西。 I've checked it a few times but maybe it needs a new set of eyes: 我已经检查了几次,但也许它需要新的眼光:

<form action="" name="costcalculator" id="costcalculator" onsubmit="return false;"> // I put name and ID to be safe

    <b>1.</b> What type of website do you need?<br>
    <input type="radio"  name="typesite" value="Standard" onclick="calculateTotal()" />
    A standard website<br>
    <input type="radio"  name="typesite" value="Full Blog" onclick="calculateTotal()" />
    A website that allows visitors to leave comments on blog posts<br>

<center><input type='submit' id='submit' value='Submit' onclick="calculateTotal()" /></center><p>

<div id="totalPrice"></div> </form>

The script: 剧本:

var typesite = new Array(); // typesite is taken from the HTML
 typesite["Standard"]=20;
 typesite["Full Blog"]=30;

function getLayoutPrice() // new name for function
{  
    var layoutPrice=0; // new name for the variable
    var theForm = document.forms["costcalculator"]; // comes from form name/ID
    var typeSite = theForm.elements["typesite"]; // new variable name and name from HTML
for(var i = 0; i < typeSite.length; i++)
{
    if(typeSite[i].checked)
    {
        layoutPrice = typesite[typeSite[i].value];
        break;
    }
}
return layoutPrice; // taken from the variable created earlier
}

function calculateTotal()
{
    var totalPrice = getLayoutPrice();

    var divobj = document.getElementById('totalPrice');
    divobj.style.display='block';
    divobj.innerHTML = "Total Price would be about $"+totalPrice;

}

function hideTotal()
{
    var divobj = document.getElementById('totalPrice');
    divobj.style.display='none';
}

Tested in current Chrome and Firefox and seems to work fine, as can be observed from running the snippet below. 在当前的Chrome和Firefox中进行了测试,并且似乎可以正常运行,从下面的代码段可以看出。

 var typesite = new Array(); // typesite is taken from the HTML typesite["Standard"]=20; typesite["Full Blog"]=30; function getLayoutPrice() // new name for function { var layoutPrice=0; // new name for the variable var theForm = document.forms["costcalculator"]; // comes from form name/ID var typeSite = theForm.elements["typesite"]; // new variable name and name from HTML for(var i = 0; i < typeSite.length; i++) { if(typeSite[i].checked) { layoutPrice = typesite[typeSite[i].value]; break; } } return layoutPrice; // taken from the variable created earlier } function calculateTotal() { var totalPrice = getLayoutPrice(); var divobj = document.getElementById('totalPrice'); divobj.style.display='block'; divobj.innerHTML = "Total Price would be about $"+totalPrice; } function hideTotal() { var divobj = document.getElementById('totalPrice'); divobj.style.display='none'; } 
 <form action="" name="costcalculator" id="costcalculator" onsubmit="return false;"> // I put name and ID to be safe <b>1.</b> What type of website do you need?<br> <input type="radio" name="typesite" value="Standard" onclick="calculateTotal()" /> A standard website<br> <input type="radio" name="typesite" value="Full Blog" onclick="calculateTotal()" /> A website that allows visitors to leave comments on blog posts<br> <center><input type='submit' id='submit' value='Submit' onclick="calculateTotal()" /></center><p> <div id="totalPrice"></div> </form> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM