2013-03-19 107 views
-2

所以我正在做一個非常基本的事情。我寫了這個代碼。我無法從中得出結果。我不確定我做錯了什麼。因爲我在學習。HTML計算器

<head> 
<script language="javascript" type="text/javascript"> 
function add(){ 
    var input1=a; 
    var input2=b; 
    var result = a+b; 
    input1 = parseInt(document.getElementById("t1").value); 
    input2 = parseInt(document.getElementById("t2").value); 
    document.write("result"); 
} 
</script> 
<title>java</title> 
</head> 
<body> 
    <form name="f1"> 
     <input type="text" id="t1" name="t1"> 
     <input type="text" id="t2" name="t2"> 
     <p> 
      <input type="button" id="add" value="add" onClick="function add();"> 
      <input type="button" id="divide" value="divide" onClick="javascript:function divide();"> 
      <input type="button" id="subtract" value="subtract" onClick="javascript:function subtract();"> 
      <input type="button" id="multiply" value="multiply" onClick="javascript:function multiply();"> 
     </p> 
    </form> 
</body> 
</html> 

但它不會產生結果。

+1

變量a和b不要選中此似乎沒有定義編輯。我想你是用正確的方式來做這件事的...... 你想先得到'parseInt(..)'的值然後再加上'document.write(input1 + input2)' – charly 2013-03-19 06:38:37

+0

不要'onClick = 「function add();」'。做'onClick =「add()」'。 – 2013-03-19 06:38:49

回答

0
<script language="javascript" type="text/javascript"> 
function add(){ 

var input1 = parseInt(document.getElementById("t1").value); 
var input2 = parseInt(document.getElementById("t2").value); 
var result = input1 +input2 ; 
document.write(result); 
} 
</script> 
+0

不工作,它根本不起作用 – user2185284 2013-03-19 07:40:45

2

結果並沒有真正寫入,而是寫入了字符串「result」。

var input1=parseInt(document.getElementById("t1").value); 
var input2=parseInt(document.getElementById("t2").value); 
var result = input1+input2; 
document.write(result); 
+1

這遠遠不是代碼中唯一的問題。 – JJJ 2013-03-19 06:40:20

+0

所以我試了document.write(result); 沒有工作。它不加,乘,除減。 – user2185284 2013-03-19 06:51:25

+0

我建議你看看jQuery,並學習基礎,以及事件處理程序是如何工作的。這將讓你以更好的方式構建你的代碼。 – AJB123 2013-03-20 03:16:17

1

修改你的函數爲:

function add(){  
    var input1 = parseInt(document.getElementById("t1").value); 
    var input2 = parseInt(document.getElementById("t2").value); 
    var result = input1+input2; 
    document.write(result); 
    } 

希望這有助於

+0

沒有工作,它只是產生什麼,當我點擊添加,乘或其他按鈕 – user2185284 2013-03-19 07:07:47

+0

看到我的答案在下面... – Ansari 2013-03-19 10:17:33

1

試試這個:

<head> 
<script language="javascript" type="text/javascript"> 
function add(){ 

    var input1 = parseInt(document.getElementById("t1").value); 
    var input2 = parseInt(document.getElementById("t2").value); 
var result = input1 +input2; 
    document.write(result); 
} 
</script> 
<title>java</title> 
</head> 
<body> 

     <input type="text" id="t1" name="t1"> 
     <input type="text" id="t2" name="t2"> 
     <p> 
      <input type="button" id="add" value="add" onClick="add();"> 
      <input type="button" id="divide" value="divide" onClick="divide();"> 
      <input type="button" id="subtract" value="subtract" onClick="subtract();"> 
      <input type="button" id="multiply" value="multiply" onClick="multiply();"> 
     </p> 

</body> 
</html> 

你可以http://htmledit.squarefree.com/