2013-11-14 109 views
0

我只是想將我的小項目轉換爲英寸,但它似乎不起作用。它所做的只是將最後的數字添加到第一個數字的末尾。我一定做錯了什麼。我只想乘以第一個數字,然後將英寸添加到該數字。謝謝Javascript addition not working out

  <!doctype> 
      <html> 
      <head> 
      <meta charset="utf-8"> 
      <title>Are you tall enough</title> 
      <script type="text/javascript"> 

      function ride(){ 
       var rideMin = 64; 

       var ht = document.getElementById("height").value; 

       if(ht >= 64){ 
        alert("You may go on the ride!!"); 
       }else{ 
        alert("Sorry, you may not go on the ride"); 
       } 
      } 

      function converter(){ 

      var aa = document.getElementById("feet").value; 
      var bb = document.getElementById("inches").value; 

      var cc = (aa * 12); 

      var resu = bb+cc; 


      document.getElementById("results").value = resu; 

      } 



      </script> 
      <style> 

      .container{ 
       width: 960px; 
      } 

      input{ 

       width:250px; 
       height: 150px; 
       margin-left: 375px; 
      } 

      img{ 
       margin-left: 300px; 
       max-width: 100%; 
       width: 40%; 
       margin-bottom: 100px; 
      } 

      h1{ 
       margin-left: 200px; 
       margin-bottom: 50px; 
       color: blue; 
      } 

      .conversion{ 
       margin: 0; 
       width: 100px; 
       height: 50px; 
      } 

      .convert{ 
       margin-left: 390px; 
      } 

      .results{ 
       width: 100px; 
       height: 50px; 
       margin-left: 438px; 
       margin-bottom: 50px; 
      } 
      </style> 

      </head> 
      <body> 
      <div class="container"> 
      <h1> Are you tall enough to ride the roller coaster</h1> 
      <img src="roller.png"> 
      <div class="convert"> 
      <h3>Convert feet to inches</h3> 
      <input class="conversion" type="text" id="feet" placeholder="Enter feet here"> 
      <input class="conversion" type="text" id="inches" placeholder="Enter inches here"> 
      <input class="conversion" type="submit" onclick="converter()"> 
      </div> 
      <div class="result"> 
      <input id="results" type="text" readonly> 
      <div class="height"> 
      <input type="text" id="height" placeholder="Please enter your height in inches please"> 
      <input type="submit" value="sumbit" onclick=ride()> 
      </div> 






      </div> 






      </body> 
      </html> 
+6

輸入值總是字符串。 '「2」+「3」'(或者甚至是「2」+ 3')是串聯的,而不是加法。根據需要使用'parseInt(bb)','parseFloat(bb)'或'+ bb'。 (我會看看是否可以找到重複。) – apsillers

+0

啊,鬆散類型語言的惡魔。我更喜歡在編譯時發現錯誤。 – Katana314

回答

0

使用parseInt函數轉換爲整數:

function converter(){ 

      var aa = parseInt(document.getElementById("feet").value, 10); 
      var bb = parseInt(document.getElementById("inches").value, 10); 

      var cc = (aa * 12); 

      var resu = bb+cc; 


      document.getElementById("results").value = resu; 

    } 
+0

我建議你'parseInt(val,10)'確保你得到正確的結果。 –

+0

非常感謝。這可能是一個愚蠢的問題,但10做什麼?我可以只做parseInt嗎? – user2684242

+0

@ user2684242它強制'parseInt'將輸入字符串視爲基數10.規範說基本系統「假定爲」10「,除非數字以字符對」0x「或」0X「開頭,在這種情況下......假設是'16'。「比較'parseInt(「0xFF」)'和'parseInt(「0xFF」,10)'。 – apsillers

0

使用這個代替:

function ride(){ 
       var rideMin = 64; 

       var ht = document.getElementById("height").value; 

       if(parseInt(ht) >= 64){ 
        alert("You may go on the ride!!"); 
       }else{ 
        alert("Sorry, you may not go on the ride"); 
       } 
      } 

      function converter(){ 

      var aa = parseFloat(document.getElementById("feet").value); 
      var bb = parseFloat(document.getElementById("inches").value); 

      var cc = (aa * 12); 

      var resu = bb+cc; 


      document.getElementById("results").value = resu; 

      } 
0

如果你不想使用parseInt,你可以使用一個加號強制將字符串轉換爲整數。

var aa = +document.getElementById("feet").value; 
var bb = +document.getElementById("inches").value; 

Fiddle