2013-05-01 125 views
0

我試圖從3個文本字段獲取數據,將它們相乘並將該值放入另一個文本字段中。Javascript - 更改文本字段的值

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <script type='text/javascript'> 
     function run() 
     { 
      var eq = document.getElementById("Eq"); 
      var m = document.getElementById("m1"); 
      var c = document.getElementById("c"); 
      var dT = document.getElementById("dT"); 
      var ek = document.getElementById("Ek"); 
      var v = document.getElementById("v"); 

      if(eq.value.length == 0) 
      { 
       var result = parseFloat(m.value) * parseFloat(c.value) * parseFloat(dT.value); 
       alert(result); 
       eq.value = result.toString(); 

      } 
      if(c.value.length == 0) 
      { 
       c.value = eq.value/(m.value * dT.value); 
      } 

     } 
     function m1change() 
     { 
      var m1 = document.getElementById("m1"); 
      var m2 = document.getElementById("m2"); 
      m2.value = m1.value; 

     } 
     function m2change() 
     { 
      var m1 = document.getElementById("m1"); 
      var m2 = document.getElementById("m2"); 
      m1.value = m2.value; 
     } 
    </script> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>Welcome - ברוכים הבאים</title> 
</head> 
<body> 
    <form action=""> 
     <h1>Eq = m * c * ΔT</h1> 
     <input type="number" max="2147483648" id="Eq" maxlength="10" /> = <input type="number" max="10" class="m" id="m1" maxlength="10" onkeyup="m1change()" /> * <input type="number" max="2147483648" id="cHeat" maxlength="10" /> * <input type="number" max="2147483648" id="dT" maxlength="10" /> 
     <h1>Ek = (m * v²)/2</h1> 
     <input type="number" max="2147483648" id="Ek" maxlength="10" /> = (<input type="number" max="10" class="m" id="m2" maxlength="10" onkeyup="m2change()" /> * <input type="number" max="2147483648" id="v" maxlength="10" /><font size="6.5">²</font> )/2 
     <h1>Eh = m * g * h</h1> 

     <input type="reset" /> <input type="button" onclick="run()" value="Calculate" /> 

     <h6>by X</h6> 
    </form> 
</body> 
</html> 

它不提示結果,所以我認爲它卡在結果的聲明。

我在做什麼錯?

謝謝。

+0

錯誤控制檯窗口中的任何錯誤? – Dolly 2013-05-01 05:06:14

+0

不是我可以看到的... – user2338361 2013-05-01 05:06:50

+0

嘗試在第一行的函數體中提醒 – Dolly 2013-05-01 05:06:51

回答

1

你試着用這條線

var c = document.getElementById("c"); 

得到的#c的價值,但它沒有的東西回來。沒有標籤,編號爲c。我想你的意思是它是

var c = document.getElementById("cHeat"); 
+0

謝謝,我只是想通了。 – user2338361 2013-05-01 05:20:31

0

嘗試尋找這個更新的run()函數...

你失蹤的文本框,並獲得.value的性能需要。 :)

function run() 
    { 
     var eq = document.getElementById("Eq").value; 
     var m = document.getElementById("m1").value; 
     var c = document.getElementById("cHeat").value; 
     var dT = document.getElementById("dT").value; 
     var ek = document.getElementById("Ek").value; 
     var m2 = document.getElementById("m2").value; 
     var v = document.getElementById("v").value; 

     console.log("eq = " + eq); 
     console.log("m = " + m); 
     console.log("c = " + c); 
     console.log("dT = " + dT); 
     console.log("ek = " + ek); 
     console.log("m2 = " + m2); 
     console.log("v = " + v); 


     if(eq.length == 0) 
     { 
      console.log('a'); 

      var result = parseFloat(m) * parseFloat(c) * parseFloat(dT); 
      alert(result); 

      eq = result.toString(); 

     } 
     if(c.length == 0) 
     { 
      console.log('b'); 

      c = eq/(m * dT); 
     } 

    } 
相關問題