2010-09-01 34 views
2

我是Javascript新手,我正在從事網上找到的練習。 問題是循環沒有從窗體中拾取值,所以運行計數永遠不會更新。任何幫助表示讚賞。循環不會加起來一個表格值的運行總數

其實還有一個問題,你有什麼想法爲什麼值沒有從檢查的輸入框中拾取?即使在迭代循環時,函數也會返回零。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 

</head> 
<body> 
<h1>Configure Your GT Super Sportscar</h1> 
<form id="orderform" action="#"> 
<table border="1"> 

<tr> 

<td><input type="radio" name="manual" checked="checked" id="manual" value="17790.00" />GT Manual</td><td>$17,790.00</td> 
</tr> 
<tr> 
<td><input type="radio" name="manual" id="auto" value="18590.00" />GT Automatic</td><td>$18,590.00</td> 
</tr> 
<tr> 
<td><input type="radio" name="manual" id="smanual" value="22455.00" />GT-S Manual</td><td>$22,455.00</td> 
</tr> 
<tr> 
<td><input type="radio" name="manual" id="sshift" value="23155.00"/>GT-S Sportshift</td><td>$23,155.00</td> 
</tr> 
</table> 
<table border="1"> 

<tr> 
<td><input type="radio" name="manual" id="combo1" value="1235.00" />Option Combo #1</td><td>$1235.00</td> 
</tr> 
<tr> 
<td><input type="radio" name="manual" id="combo2" value="3354.00" />Option Combo #2 
<ul> 
<li>Rear Spoiler and Fog Lamps</li> 
<li>Keyless Entry</li> 
<li>Power Tint and Side Moonroof</li> 
<li>Power Windows, Doors, and Cruise Control</li> 
</ul> 
</td> 
<td>$3354.00</td> 
</tr> 
<tr> 
<td><input type="radio" name="manual" id="nocombo" value="0" />No Combo</td><td>$0</td> 
</tr> 

</table> 
<table border="1"> 

<tr> 

<td><input type="checkbox" name="amen" id="cac" value="550.00"/>CD Autochanger</td><td>$550.00</td> 
</tr> 
<tr> 
<td><input type="checkbox" name="amen" id="vss" value="399.00"/>VIP Security System</td><td>$399.00</td> 
</tr> 
<tr> 
<td><input type="checkbox" name="amen" id="adm" value="295.00"/>Auto Dimming Mirror</td><td>$295.00</td> 
</tr> 

</table> 
<table border="1"> 

<tr> 
<td><input type="text" name="price" id="price" /></td><td><input type="button" onclick="showit()" value="Calculate Total" /></td> 
</tr> 
</table> 
</form> 
<script type="text/javascript"> 
/** 
* @author Isaac's 
*/ 
function getval(){ 
var options = document.forms["orderform"].manual; 
var optionslength = options.length; 
var total = 0; 
for (var i = 0; i &lt; optionslength; i++) { 
if (options[i].checked) { 
options[i].value += total; 
} 
return total; 
} 
} 
var result1 = getval(); 
function getval2(){ 
var total=0; 
var checkboxes = document.forms["orderform"].amen; 
for (var i = 0; i &lt; checkboxes.length; i++) { 
checkboxes[i].value += total; 
} 
return total; 
} 

var result2 = getval2(); 

function showit(){ 
var total = parseFloat(result1) + parseFloat(result2) 
alert(total); 
} 

</script> 
</body> 
</html> 
+0

您可以請張貼您的html以及? – Munzilla 2010-09-01 15:45:30

+0

html自動呈現,我只顯示代碼? – Isaac 2010-09-01 18:03:12

+0

縮進代碼四個空格('101010'按鈕)。降價提示:http://stackoverflow.com/editing-help – McDowell 2010-09-01 19:43:56

回答

2

該代碼有多個問題。

這是working jsFiddle example你可以玩。

有4從工作停止你的代碼的大問題:

  1. 在您若循環,當你想添加到你寫的總:

    options[i].value += total; 
    

    這將在options[i].value行動。你想改變total,所以你應該寫

    total += parseFloat(options[i].value); 
    
  2. getval()返回內部 for循環是這樣的:

    function getval(){ 
        var options = document.forms["orderform"].manual; 
        var optionslength = options.length; 
        var total = 0; 
        for (var i = 0; i &lt; optionslength; i++) { 
         if (options[i].checked) { 
          options[i].value += total; 
         } 
         return total; // <======= THIS IS STILL INSIDE THE FOR LOOP!!!!!!!! 
        } 
    } 
    

    你想總你的計算,所以像這樣:

    function getval(){ 
        var options = document.forms["orderform"].manual; 
        var optionslength = options.length; 
        var total = 0; 
        for (var i = 0; i &lt; optionslength; i++) { 
         if (options[i].checked) { 
          total += parseFloat(options[i].value); 
         }    
        } 
        return total; // <===== ALL CALCULATIONS ARE COMPLETED, SO WE CAN RETURN. 
    } 
    
  3. 最後在getval2()中,您忘記檢查這些框是否在將其添加到總數之前進行檢查。所以你總是得到相同的總數。您檢查是否在getval()中檢查了這些框。在getval2()中使用相同的方法。

  4. 當你得到options[i].value你會得到一個字符串。在將它們添加到total之前,您應該將它們轉換爲數字。只有在總數返回後才能轉換爲數字,屆時會發生各種有趣的連接。看看我在上面的代碼片段和jsFiddle中使用parseFloat()的位置。

0

在這段代碼中,你永遠不會做,總任何事情,它始終爲0。

function getval2(){ 
var total=0; 
var checkboxes = document.forms["orderform"].amen; 
for (var i = 0; i < checkboxes.length; i++) { 
checkboxes[i].value += total; 
} 
return total; 
} 

此外,一個提示,可以幫助你在調試這些類型的東西(和使代碼更好一般)是嘗試使用indentation

+0

感謝您的答案。這是縮進,但格式化丟失。無論如何,你說我對總變量什麼都不做,所以腳本做的是什麼。特別是在這條線上: 複選框[i]。值+ =總計; 如果這有助於我的這個代碼的意圖是循環,並獲得所選無線電和複選框的值。 – Isaac 2010-09-01 17:53:32

+0

@Isaac - 你聲明並初始化總數爲0.然後你循環複選框並將總數添加到複選框[i] .value。但總是總是0,因爲你永遠不會在任何地方改變它的價值。那麼你的意圖究竟是什麼?如果您試圖獲得複選框的值,那麼爲什麼要添加總數?只需獲得價值。 – dcp 2010-09-01 18:36:56

+0

我的想法是,既然我有兩套無線電盒,我會循環所有這些盒子,因爲兩套都有不同的名稱,我將它們加在一起(各自的值)。總計充當表單中所有選定值的佔位符。 – Isaac 2010-09-01 18:52:27

相關問題