2016-11-10 23 views
-2

我正在做一個轉換器的項目。當我在數字框中輸入一個數字時,它在框2中顯示正常。但是,當我嘗試在box2中輸入數字時,它不會在box1中顯示答案。誰能幫我?我不知道jquery,我是HTML和JavaScript的入門者。希望有人能幫助我。由於轉換盒,如何使兩個輸出盒工作?

function NumberToSquare(num) 
 
{ 
 
var sqrt; 
 
sqrt = Math.sqrt(num); 
 
return sqrt; 
 
} 
 

 
function SquareToNumber(sqrt) 
 
{ 
 
var num; 
 
num = Math.pow(sqrt,2); 
 
return num; 
 
}
<html> 
 
<head> 
 
<title>Program Assignment 5</title> 
 

 
<script type = text/javascript src = "calculate.js"> </script> 
 

 
<script type="text/javascript"> 
 
function ConvertToSqrt() 
 
    //Assumes: Number contains a number 
 
    //Results: displays the square root in box2 
 
    { 
 
    num = parseFloat(document.getElementById('box1').value); 
 
    sqrt = NumberToSquare(num); 
 
    box2.value=sqrt; 
 
    } 
 
    
 
    function ConvertToNum() 
 
    //Assumes: Square contains a number of square root 
 
    //Results: displays the number in box1 
 
    { 
 
    sqrt = parseFloat(document.getElementById('box2').value); 
 
    num = SquareToNumber(sqrt); 
 
    box1.value=num; 
 
    } 
 
</script> 
 

 
</head> 
 

 
<body> 
 
<div style="border: solid; width: 300px; background-color: #83CAFF"> 
 

 
<table> 
 
<th></th> <th>Square Root Calculation</th> 
 
<tr> 
 
<th>Enter Number:</th><th><input type="number" id="box1" value=""></th> 
 
<tr> 
 
<th>SquareRoot:</th><th><input type="number" id="box2" value=""></th> 
 
<tr> 
 
<th></th><th><input type="button" value="Calculate" onclick="ConvertToSqrt(); ConvertToNum();"> 
 
</table> 
 
</div>

+0

如果盒1是用於盒2的輸入,那麼如何可以框2是盒1的輸入在同一時間?你需要一種方法來告訴你的代碼使用哪一個作爲源代碼。也許是一個'ToSqrt'按鈕和一個'ToSquared'按鈕,而不是隻有一個按鈕來嘗試去做。 – Sam07

+0

所以我應該做另一個按鈕?我的老師要求我們使用返回值函數,但我不太確定那是什麼。 –

+0

我從來沒有聽說過任何叫做'返回值'的函數,但是如果我不得不猜測他/她意味着你要使用一個函數返回一個你已經存在的值。 – Sam07

回答

0

如果有幫助的任何,這裏有一個快速的樣品。這並不漂亮,但你可以拿出你需要的東西,並用你自己的東西來使用它。

<html> 
 
<head> 
 
    <title></title> 
 
    <script> 
 
     function calc(which) { 
 
      var newVal; 
 
      if (which == "sqrt") { 
 
       newVal = parseFloat(document.getElementById("num").value); 
 
       newVal = Math.sqrt(newVal); 
 
      } 
 
      else if (which == "num") { 
 
       newVal = parseFloat(document.getElementById("sqrt").value); 
 
       newVal = Math.pow(newVal, 2); 
 
      } 
 

 
      document.getElementById(which).value = newVal; 
 
     } 
 
    </script> 
 
</head> 
 
<body> 
 
    <div style="width:400px;padding:25px;display:inline-block;line-height:150%;background-color:#83CAFF;font-weight:bold;border:solid 1px black;"> 
 
     <div style="width:200px;float:left;"> 
 
      <label>Number:</label> 
 
      <br /> 
 
      <label>SquareRoot:</label> 
 
     </div> 
 
     <div style="width:200px;float:right;"> 
 
      <input type="number" id="num"/> 
 
      <br /> 
 
      <input type="number" id="sqrt"/> 
 
     </div> 
 
     <button type="button" style="width:100%;" onclick="calc('sqrt');">Calc Sqrt</button> 
 
     <button type="button" style="width:100%;" onclick="calc('num');">Calc Num</button> 
 
    </div> 
 
</body> 
 
</html>

+0

謝謝:)我會嘗試那麼。 –