這樣寫為什么不顯示結(jié)果?
<!DOCTYPE html>
<html>
?<head>
? <title> 事件</title>?
? <script type="text/javascript">
?? function count(){
?????? var t1=document.getElementById("txt1").value
??? //獲取第一個輸入框的值
??????? var t1=document.getElementById("txt1").value
?//獲取第二個輸入框的值
???? var t3=document.getElementById("select").value
?//獲取選擇框的值
?var t
???? switch(t3)
???? {
???????? case "+":
????????????? t=parseInt(t1)+parseInt(t2);
????????????? break;
???????? case "-":
???????????? t=parseInt(t1)-parseInt(t2);
???????????? break;
???????? case "*":
????????????? t=parseInt(t1)*parseInt(t2);
????????????? break;
????????? case "/":
?????????????? t=parseInt(t1)/parseInt(t2);
?????????????? break;
???? }
????
?//獲取通過下拉框來選擇的值來改變加減乘除的運算法則
?document.getElementById("fruit").value=t;
??? //設置結(jié)果輸入框的值
???????
?? }
? </script>
?</head>
?<body>
?? <input type='text' id='txt1' />
?? <select id='select'>
??<option value='+'>+</option>
??<option value="-">-</option>
??<option value="*">*</option>
??<option value="/">/</option>
?? </select>
?? <input type='text' id='txt2' />
?? <input type='button' value=' = '? onclick="count()"/> <!--通過 = 按鈕來調(diào)用創(chuàng)建的函數(shù),得到結(jié)果-->
?? <input type='text' id="fruit" />??
?</body>
</html>
2017-06-03
?var t1=document.getElementById("txt1").value
??? //獲取第一個輸入框的值
??????? var t1=document.getElementById("txt1").value
?//獲取第二個輸入框的值
-------------------------------------------------------
首先,你這里寫錯,應該是獲取第二個輸入框,var t2=......"txt2"
--------------------------------------------------------------------
2017-06-03
不用每次都聲明一個變量 加一個var,?? 同事 每個代碼結(jié)束段, 一定要有;
2017-06-03
我這樣寫,比你那樣寫節(jié)省 資源, 直接在頂部 定義 全局變量了。
2017-06-03
function count(){
??? var txt1,txt2,fruit; ?
??? //獲取第一個輸入框的值
???? txt1 = document.getElementById('txt1').value;
?? ?//獲取第二個輸入框的值
?? ? txt2 = document.getElementById('txt2').value;
?? ?//獲取選擇框的值
?? ? select = document.getElementById('select').value;
?? ?
?? ?
?? ?//獲取通過下拉框來選擇的值來改變加減乘除的運算法則
?? ?switch(select){
?? ???? case '+':
?? ???????? fruit = parseInt(txt1)+parseInt(txt2);
?? ???????? break;
?? ???? case '-':
?? ???????? fruit = parseInt(txt1)-parseInt(txt2);
?? ???????? break;
?? ???? case '*':
?? ???????? fruit = parseInt(txt1)*parseInt(txt2);
?? ???????? break;
?? ???? case '/':
?? ???????? fruit = parseInt(txt1)/parseInt(txt2);
?? ???????? break;
?? ???? default:
?? ???????? document.write('對不起,您的輸入有誤!');? ?
?? ?}
?? ?
??? //設置結(jié)果輸入框的值
???? document.getElementById('fruit').value=fruit;
?? ?
??? ?
?? }
2017-06-03
var t 這個變量后面加上分號 ,包括每個document.getElementById("txt1").value 類似的取值代碼后面,加上分號!