2016-04-14 125 views
0

我很新的Javascript和此網站,但我正在尋找我的項目的幫助。麻煩計算 - Javascript形式

到目前爲止這是一個有點粗糙和不完整,但我不能繼續前進,直到我找出如何實際得到計算顯示。如果我能得到幫助,找出爲什麼第一個蘋果總計不算,那太棒了!

這裏是我的完整的HTML頁面(工作正在進行中):

<html> 
    <head> 
      <title>Order Form</title> 
      <style> 
        .inBox { width:60px; text-align:right; border: 1px solid green; } 
        .thWide { width:80px; text-align:right; } 
      </style> 
      <script type="text/javascript"> 
        function compute() 
        { 
          // Pointers to red asterisks 
          var spnA = document.getElementById("spnA"); 
          var spnP = document.getElementById("spnP"); 
          var spnG = document.getElementById("spnG"); 

          // Assume no errors yet 
          var message = ""; 
          spnL.style.display = "none"; 
          spnW.style.display = "none"; 
          spnH.style.display = "none"; 



          var apple = form1.txtA.value; 
          if (apple == "") 
            apple = 1; 
          else 
            apple = parseFloat(apple); 
          if ( isNaN(apple) ) 
          { 
            spnA.style.display = "inline"; 
            message = message + "Apple is bad\n"; 
            form1.txtA.select(); 
          } 

          var pear = form1.txtP.value; 
          if (pear == "") 
            pear = 1; 
          else 
            pear = parseFloat(pear); 
          if ( isNaN(pear) ) 
          { 
            spnP.style.display = "inline"; 
            message = message + "Pear is bad\n"; 
            form1.txtP.select(); 
          } 

          var grape = form1.txtG.value; 
          if (grape == "") 
            grape = 1; 
          else 
            grape = parseFloat(grape); 
          if ( isNaN(grape) ) 
          { 
            spnG.style.display = "inline"; 
            message = message + "Grape is bad\n"; 
            form1.txtG.select(); 
          } 

          if ( message != "") 
          { 
            // Show error and clear subA 
            alert(message); 
            form1.txtsubA.value = ""; 
          } 
          else 
          { 
            // Compute subA 
            var subA = length * 5.49; 
            form1.txtsubA.value = subA; 
            form1.txtA.select(); 
          } 
        } 
        </script> 
    </head> 
    <body> 
      <form id="form1"> 
        <table border="2"> 
          <tr><th colspan="4">Volume Box</th></tr> 
          <tr><th>Quantity</th><th>Item</th><th>Unit Prics</th><th>Totals</th></tr> 
          <tr> 
            <th class="thWide"> 
              <span id="spnA" style="color:red; font-weight:bold; display:none;">*</span> 
              <input type="text" id="txtA" class="inBox" tabindex="1" autofocus /> 
            </th><td>Apples</td><td>$5.49</td> 
            <th style="width:80px;"><input type="text" id="txtsubA" class="inBox" readonly /></th> 
          </tr><tr> 
            <th class="thWide"> 
              <span id="spnP" style="color:red; font-weight:bold; display:none;">*</span> 
              <input type="text" id="txtP" class="inBox" tabindex="1" autofocus /> 
            </th><td>Pears</td><td>$6.49</td> 
            <th style="width:80px;"><input type="text" id="txtsubP" class="inBox" readonly /></th> 
          </tr><tr>   
            <th class="thWide"> 
             <span id="spnG" style="color:red; font-weight:bold; display:none;">*</span> 
             <input type="text" id="txtG" class="inBox" tabindex="1" autofocus /> 
            </th><td>Grapes</td><td>$7.49</td> 
            <th style="width:80px;"><input type="text" id="txtsubG" class="inBox" readonly /></th> 
          </tr> 
          <tr><th colspan="4"> 
            <input type="button" value="Compute" onclick="compute();" tabindex="4" /> 
          </th></tr> 
        </table> 
      </form> 
    </body> 
    </html> 
+0

您能否更新您的問題並添加示例輸入,您收到的輸出以及您期望收到的輸出。 –

+0

您尚未定義spnL,spnW,spnH –

回答

0

在瀏覽器控制檯可以在任何診斷問題非常有幫助。例如,你的代碼給出了這樣的錯誤在控制檯:

test.html:18 Uncaught ReferenceError: spnL is not defined 

我假設你的意思是這個部分:

spnL.style.display = "none"; 
spnW.style.display = "none"; 
spnH.style.display = "none"; 

是:

spnA.style.display = "none"; 
spnP.style.display = "none"; 
spnG.style.display = "none"; 

至於你問題,問題出在這個部分:

// Compute subA 
var subA = length * 5.49; 

長度不定義任何地方,你大概的意思:

// Compute subA 
var subA = apple * 5.49; 

然後你也可能會想從

form1.txtsubA.value = subA; 

改變後,該行

form1.txtsubA.value = subA.toFixed(2); 

它只會顯示2位小數。

0

擺脫(或註釋掉):

spnL.style.display = "none"; 
spnW.style.display = "none"; 
spnH.style.display = "none"; 

收集的形式提供的值(加 「.value的」):

// Pointers to red asterisks 
var spnA = document.getElementById("txtA").value; 
var spnP = document.getElementById("txtP").value; 
var spnG = document.getElementById("txtG").value; 

交換長度申報值:

// Compute subA 
var subA = spnA * 5.49; 

享受!