2017-06-29 84 views
0

我遇到了一個簡單的價格計算器問題。我已經學會了Javascript,並且我完成了所有工作,看起來很好,但是我肯定錯過了一些東西,因爲最終的價格沒有出現。我檢查了幾次,但也許它需要一套新的眼睛:Javascript價格計算器的問題 - 價格不顯示

<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> 

腳本:

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'; 
} 
+1

'layoutPrice = typesite [typeSite [I]。價值];'我會改變這些變量中的一個的名稱。這很難理解。 –

回答

0

經測試,在當前的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>

+0

謝謝!我又看了一遍,顯然我的腳本沒有被調用,但是如果我把表單和腳本放在同一頁面上,代碼就可以工作。我確實在頁面的頭部有,但同時我已將腳本放在與表單相同的頁面上。 – Lilly