2013-02-13 140 views
0

我不能讓一切都集中。 這就是我得到:JS平均計算器

xxx xxx xxx xxx xxx 

我要的是

xxx 
xxx 
xxx 
xxx 
xxx 

在網頁horizo​​nally中心

我也不能得到平均的功能,幹活不知道我在哪裏出錯了。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>Average Calculator</title> 
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> 
<h1 style="text-align:center;">My Average Calculator</h1> 
<script type="text/javascript"> 
function getTotal() { 
var form = document.getElementById('number'); 
var numb1 = parseInt(form.numb1.value); 
var numb2 = parseInt(form.numb2.value); 
var numb3 = parseInt(form.numb3.value); 
var numb4 = parseInt(form.numb4.value); 
var numb5 = parseInt(form.numb5.value); 
var total = document.getElementById('total'); 
var average = document.getElementById('average'); 
if (!numb1) { 
    numb1 = 0; 
} 

if (!numb2) { 
    numb2 = 0; 
} 

if (!numb3) { 
    numb3 = 0; 
} 

if (!numb4) { 
    numb4 = 0; 
} 
if (!numb5) { 
    numb5 = 0; 
} 

total.innerHTML = 'Total: ' + (numb1 + numb2 + numb3 + numb4 + numb5); 
average.innerHTML = 'Average: ' + (total/5); 
} 
</script> 
</head> 
<form id="number"> 
<body> 
First Number: <input type="text" name="numb1" onkeyup="getTotal();" /> 
Second Number: <input type="text" name="numb2" onkeyup="getTotal();" /> 
Third Number: <input type="text" name="numb3" onkeyup="getTotal();" /> 
Fourth Number: <input type="text" name="numb4" onkeyup="getTotal();" /> 
Fifth Number: <input type="text" name="numb5" onkeyup="getTotal();" /> 
    <div id="total">Total:  </div> 
    <div id="average">Average: </div> 
</body> 
</html> 

回答

1

total/5,其中total是由ID取出的元件你。難怪它不工作;)

此外,你可能想了解循環。迭代,而不是numb1numb5。在每個輸入標籤 結束

<fieldset id="numbers"><legend>Numbers</legend> 
    First number: <input type="number" onkeyup="getTotal();" onchange="getTotal();" /><br /> 
    Second number: <input type="number" onkeyup="getTotal();" onchange="getTotal();" /><br /> 
    Third number: <input type="number" onkeyup="getTotal();" onchange="getTotal();" /><br /> 
    Fourth number: <input type="number" onkeyup="getTotal();" onchange="getTotal();" /><br /> 
    Fifth number: <input type="number" onkeyup="getTotal();" onchange="getTotal();" /> 
</fieldset> 
<div id="total">Total: --</div> 
<div id="average">Average: --</div> 
<script type="text/javascript"> 
    function getTotal() { 
     var inputs = document.getElementById('numbers').getElementsByTagName('input'), 
      count = inputs.length, i, total = 0; 
     for(i=0; i<count; i++) total += parseInt(inputs[i].value || "0",10); 
     document.getElementById('total').firstChild.nodeValue = "Total: "+total; 
     document.getElementById('average').firstChild.nodeValue = "Average: "+(total/count); 
    } 
</script> 
+0

哇,這是很多簡單:

像這樣的事情會很好地工作。是的,下週我們不會進入循環。謝謝你的幫助 – user2069296 2013-02-14 01:04:18

+0

我必須使用: onchange事件處理程序 calcAvg() performCalc() calcResult averageCalc() parseInt函數() 這很爛,因爲我得到了它,我喜歡的方式和現在我得改變它 – user2069296 2013-02-14 02:33:22

0

附加BR標籤<br/>和改變這種

average.innerHTML = 'Average: ' + ((numb1 + numb2 + numb3 + numb4 + numb5)/5); 
+0

我試過這個,但結果仍然一樣,但是非常感謝你 – user2069296 2013-02-14 00:58:19

+0

很快我發現了一個錯誤謝謝你 – user2069296 2013-02-14 01:06:50