2014-12-02 60 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