2013-12-17 68 views
0

請幫助我編寫此表單。我只需要「總計」框來計算某人點擊單選按鈕的時間。我從另一個問題收到了部分答案,我試圖按照答案,但它不起作用。我明顯錯過了其他的東西,而且我對JS的知識還不夠深入。你能看看我的代碼,並告訴我我錯過或做錯了什麼?謝謝(我的雜亂格式道歉)Javascript「總計」框未合併

<html> 
<head> 
    <title> HTML and JavaScript </title> 
    <script type="text/javascript"> 

    function towerPic() 
    {} 
    function doClear() 
    { 
     document.AddressForm.customer.value = ""; 
     document.AddressForm.address.value = ""; 
     document.AddressForm.city.value= ""; 
     document.AddressForm.state.value = ""; 
     document.AddressForm.zip.value = ""; 
     document.AddressForm.phone.value= ""; 
     document.AddressForm.email.value= ""; 
     document.AddressForm.total.value= "0.00"; 

     document.ComputerForm.cases[0].checked = false; 
     document.ComputerForm.cases[1].checked = false; 
     document.ComputerForm.cases[2].checked = false; 

     document.ComputerForm.monitors[0].checked = false; 
     document.ComputerForm.monitors[1].checked = false; 
     document.ComputerForm.monitors[2].checked = false; 

     document.ComputerForm.printers[0].checked = false; 
     document.ComputerForm.printers[1].checked = false; 
     document.ComputerForm.printers[2].checked = false; 

     return; 
    } 

    var prices = new Object(); 
    function updateTotal() { 
     var total = 0.00; 
     for (var price in prices) { 
      total += price; 
     } 
     document.getElementById("total").value = total; 
    } 
    </script> 
</head> 
<body> 
<h1 align="center">Computer System Order Form</h1> 
<table border="black" cellpadding="10px" align="center"> 
    <tr> 
     <td> 
      <table cellpadding="10px"> 
      <form name="ComputerForm"> 
      <tr> 
       <td> 
        Computer Case Style:</br> 
        <input type="radio" onclick="towerPic()" name="cases" onchange="javascript:prices['cases'] = parseInt(500.00); updateTotal();"id="desktop" value="500.00" /> Desktop Case (500.00) </br> 
        <input type="radio" onclick="towerPic()" name="cases" onchange="javascript:prices['cases'] = parseInt(600.00); updateTotal();"id="mini" value="600.00" /> Mini-Tower Case (600.00) </br> 
        <input type="radio" onclick="towerPic()" name="cases" onchange="javascript:prices['cases'] = parseInt(700.00); updateTotal();" id="full" value="700.00" /> Full-Tower Case (700.00) </br> 
       </td> 
       <td> 
        <img src="case.jpg" width="125" height = "125" id="pcCase" > 
       </td> 
      </tr> 
      <tr> 
       <td> 
        Computer Monitor: <br/> 
        <input type="radio" name="monitors" value="250.00" /> 17" LCD Flat Screen (250.00) </br> 
        <input type="radio" name="monitors" value="300.00" /> 19" LCD Flat Screen (300.00) </br> 
        <input type="radio" name="monitors" value="350.00" /> 21" LCD Flat Screen (350.00) </br> 
       </td> 
       <td> 
        <img src="monitor.jpg" width="125" height = "125" alt="pc monitor" /> 
       </td> 
      </tr> 
      <tr> 
       <td> 
        Computer Printer:<br/> 
        <input type="radio" name="printers" value="100.00" /> Inkjet Printer (100.00) </br> 
        <input type="radio" name="printers" value="250.00" /> Laser Printer (250.00) </br> 
        <input type="radio" name="printers" value="350.00" /> Color Printer (350.00) </br> 
       </td> 
       <td> 
        <img src="printer.jpg" width="125" height = "125" alt="pc printer" /> 
       </td> 
      </tr> 
      </form> 
      </table> 
     </td> 
     <td> 
      <table padding="10px"> 
      <tr> 
       <form name="AddressForm"> 
       <td>Total System Price:</td> 
       <td>$<input type="text" name="total" readonly value="0.00" size="8"></td> 
      </tr> 
      <tr> 
       <td>&nbsp;</td> 
      </tr> 
      <tr> 
       <td>&nbsp;</td> 
      </tr> 
      <tr> 
       <td>&nbsp;</td> 
      </tr> 
      <tr> 
       <td>Full Name:</td> 
       <td><input type="text" name="customer" /></td> 
      </tr> 
      <tr> 
       <td>Street Address:</td> 
       <td><input type="text" name="address" /></td> 
      </tr> 
      <tr> 
       <td>City:</td> 
       <td><input type="text" name="city" /></td> 
      </tr> 
      <tr> 
       <td>State:</td> 
       <td><input type="TEXT" name="state" size="2"></td> 
      </tr> 
      <tr> 
       <td>Zip:</td> 
       <td><input type="text" name="zip" /></td> 
      </tr> 
      <tr> 
       <td>Phone Number:</td> 
       <td><input type="text" name="phone" /></td> 
      </tr> 
      <tr> 
       <td>Email Address:</td> 
       <td><input type="text" name="email"></td> 
      </tr> 
      </form> 
      <tr> 
       <td>&nbsp;</td> 
      </tr> 
      <tr> 
       <td> 
        <input type="button" value="Submit Order" onClick="doSubmit()" /> 
       </td> 
       <td> 
        <input type="button" value="Clear Form" onClick="doClear()" /> 
       </td> 
      </tr> 
      </table> 
     </td> 
    </tr> 
</table> 
</body> 
</html> 
+1

您似乎沒有在「價格」對象中添加任何內容......無論如何,總是使用整數,特別是在處理金錢時。 –

+0

您也沒有在「total」測試框中設置'''id'''屬性。 –

回答

1

您應該使用ID而不是僅僅一個名字:

$<input type="text" name="total" id="total" readonly value="0.00" size="8"> 

此外,您可能需要做這樣的事情來填充數組prices

<input type="radio" onchange="updateTotal(this.id, this.value)" id="desktop" value="500.00" /> Desktop Case (500.00) </br> 

,這樣就可以存儲並在你的腳本更容易均勻地使用這些值。