請問這里哪里錯了??我按了=按鈕無法輸出結果。。。
? ? ? ?var x = getElementById("txt1").value;
? ? ? ?var y = getElementById("txt2").value;
? ? ? ?var option = getElementById("select").value;
? ? ? ?var result;
? ? ? ?function getResult(){
? ? ? ? ? ?if(option=='+'){
? ? ? ? ? ? ? ?result= x+y;
? ? ? ? ? ?}else if(option=='-'){
? ? ? ? ? ? ? ?result= x-y;
? ? ? ? ? ?}else if(option=='*'){
? ? ? ? ? ? ? ?result= x*y;
? ? ? ? ? ?}else{
? ? ? ? ? ? ? ?result= x/y;
? ? ? ? ? ?}
? ? ? ? ? ?document.getElementById("fruit").value=result;
2017-01-29
在這里添加 onclick="getResult()" :
<input type='button' value=' = ' onclick="count()"/> <!--通過 = 按鈕來調用創建的函數,得到結果-->?
然后還有就是,+號問題,由于JS默認取出來的值不確定是否字符串,當使用+號時,需要將其轉成數字,result = Number(x) + Number(y); 這樣才能得到正確的結果。
2017-02-05
getElementById('');這個地方應該是單引號‘? ’而不是雙引號“? ”