2014-12-02 58 views
0

我一直在試圖添加下面代碼中顯示的兩個不同變量,但無論我做了什麼,結果都是楠,這裏有什麼錯誤嗎?我嘗試了一段時間,但我找不到任何錯誤的代碼!在楠中添加兩個變量

      <script type="text/javascript"> 
          function showEstimate() { 
          switch (document.getElementById("cboCar").value) { 
          case "A": 
          document.getElementById("imgCar").src= "images/Accord.jpg"; 
          carPrice = document.getElementById("txtEstPr").value; 
          break; 
          case "C": 
          document.getElementById("imgCar").src= "images/Civic.jpg"; 
          carPrice = document.getElementById("txtEstPr").value; 
          break; 
          } // end of switch block. 
          //here, the UI has done processing cars pics 
          //display the price for cars 
          document.getElementById("txtEstPr").value= "$" + carPrice; 
          // end of the function named 



          { 
           var vchkSt= (300.99).toFixed(); 
           var vchkLe= (750.99).toFixed(); 
           var vchkGps= (1600.00).toFixed(); 
           var tTotal = 0; 
           carPrice = Number(document.getElementById("txtBasePr").value); 


           if (document.getElementById("chkSt").checked) 
           tTotal += parseFloat(carPrice) + parseFloat(vchkSt); 

           if (document.getElementById("chkLe").checked) 
           tTotal += parseFloat(carPrice) + parseFloat(vchkLe); 

           if (document.getElementById("chkGps").checked ==true) 
           tTotal += parseFloat(carPrice) + parseFloat(vchkGps); 


           //displaying the total 
           document.getElementById("txtEstPr").value = "$" + tTotal; 
           } 
           } // end of function showEstimate 

           </script> 
+0

錯誤:'(300.99).toFixed;'和'Number(carPrice = document.getElementById(「txtEstPr」)value);' – epascarello 2014-12-02 21:16:49

+0

你檢查過什麼'carPrice'?如果getElementById()失敗(例如錯誤的id),則carPrice將變爲null。 – 2014-12-02 21:16:51

+0

我正在將carPrice設置爲getElememtbyId的值(「txtEstpr」) – sunnybouy91 2014-12-02 21:34:42

回答

3

你不是調用toFixed功能,而不是你分配功能chkStchkLechkGps

var chkSt= (300.99).toFixed; 
var chkLe= (750.99).toFixed; 
var chkGps= (1600.00).toFixed; 

相反,它應該是:

var chkSt= (300.99).toFixed(); 
var chkLe= (750.99).toFixed(); 
var chkGps= (1600.00).toFixed(); 

另外

Number(carPrice = document.getElementById("txtEstPr").value); 

沒有任何意義。無需將作業包裝在Number函數中。它看起來像你想的值賦給一個編號carPrice,這將是做簡單的:

carPrice = +document.getElementById("txtEstPr").value; 

carPrice = Number(document.getElementById("txtEstPr").value); 

此外,你重新分配tTotal以後每次複選框。你或許應該使用:

if (document.getElementById("chkSt").checked) { 
    tTotal += parseFloat(carPrice) + parseFloat(chkSt); 
    // ^
} 

var tTotal = 0初始化變量。

+0

仍有問題 – epascarello 2014-12-02 21:14:40

+0

我仍然收到同樣的問題 – sunnybouy91 2014-12-02 21:29:46

+0

@ sunnybouy91,我建議您投入一些時間來改進您的問題。花時間在[jsfiddle](http://jsfiddle.net)等服務上創建[SSCCE](http://sscce.org/),甚至在您的問題中使用[snippet feature](http ://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/)。 – zzzzBov 2014-12-02 21:49:42