請(qǐng)問(wèn)這里哪里錯(cuò)了??我按了=按鈕無(wú)法輸出結(jié)果。。。
? ? ? ?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()"/> <!--通過(guò) = 按鈕來(lái)調(diào)用創(chuàng)建的函數(shù),得到結(jié)果-->?
然后還有就是,+號(hào)問(wèn)題,由于JS默認(rèn)取出來(lái)的值不確定是否字符串,當(dāng)使用+號(hào)時(shí),需要將其轉(zhuǎn)成數(shù)字,result = Number(x) + Number(y); 這樣才能得到正確的結(jié)果。
2017-02-05
getElementById('');這個(gè)地方應(yīng)該是單引號(hào)‘? ’而不是雙引號(hào)“? ”