2015-03-03 44 views
-1

我一直在嘗試爲類做這件事,但我似乎無法讓它工作。我需要有單選按鈕來選擇4個硬幣中的一個。 Q,d,N,P。然後選擇一個值爲10的選擇框。當我改變選擇時,Java需要添加選定數量的硬幣並顯示總計,並顯示許多硬幣。我有添加部分,但是當我添加循環時,它會中斷項目的添加部分。我不知道我的循環是否正確,但現在我想知道爲什麼我的代碼在添加循環時中斷?代碼我到目前爲止。JavaScript代碼與循環中斷

<script type="text/javascript"> 
    function doCalculate() { 
     //Variable for calculation 
     Q = document.getElementById('txtMoney').value; 

     //If statements for selecting the correct radiobutton 
     if (document.getElementById('rbQ').checked) { 
      answer = Q * .25; 
     } 
     if (document.getElementById('rbD').checked) { 
      answer = Q * .10; 
     } 
     if (document.getElementById('rbN').checked) { 
      answer = Q * .05; 
     } 
     if (document.getElementById('rbP').checked) { 
      answer = Q * .01; 
     } 


     //show results 
     document.getElementById('results').innerHTML = "$ " + answer + "<br>Dollars and Cents" 


     //variables for coins using ID txtMoney from Select 
     loopCounter = document.getElementById('txtMoney').value 

     //If radio of coin is checked and loopcounter is higher then 0 run for loop 
     if (document.getElementById('rbQ').checked && loopCounter > 0;) { 
      document.getElementById('quarter').src = "quarter.gif"; 

     } 

     resultsString = "" 

     //for loop to count via select box value, and display that many images 
     // of which ever coins radio is checked. 
     for (x = 0; x < loopCounter; x++) { 
      resultsString = loopCounter + quarter.gif 
      //write to results2 via innerHTML 
      document.getElementById('results2').innerHTML = resultsString; 
     } 
    } 
</script> 
+0

哪裏是Java代碼? – Teemu 2015-03-03 09:02:00

+0

您在初始化'loopCounter'後缺少分號。 'document.getElementById('results)''後面還有一個。乍一看還有其他一些......並且在if語句之一中有一個不需要的分號。同時請注意JavaScript!= Java – treegarden 2015-03-03 09:05:51

+0

您可以發佈隨附的HTML嗎? – rmorrin 2015-03-03 09:08:16

回答

0

你缺少字符串分隔符,當你把值賦給resultsString,所以代碼將循環在quarter對象,這自然不存在gif財產。

resultsString = loopCounter + "quarter.gif"; 

你有一個分號if條件裏面太多的循環之前:

if (document.getElementById('rbQ').checked && loopCounter>0) { 
+0

謝謝你Guffa。 – 2015-03-04 09:53:56