function?count(){
???????
???var?first=parselnt(document.getElementById("txt1").value);//獲取第一個輸入框的值
var?second=parselnt(document.getElementById("txt2").value);//獲取第二個輸入框的值
var?select=document.getElementById("select").value;//獲取選擇框的值
var?result;//獲取通過下拉框來選擇的值來改變加減乘除的運算法則//設置結果輸入框的值?
????switch(select){
??????case?"+":
??????????result=first+second;
??????????break;
??????case?"-":
??????????result=first+second;
??????????break;
??????case?"*":
??????????result=first+second;
??????????break;
??????case?"/":
??????????result=first+second;
??????????break;
??????default:
??????????alert("error");}
?????document.getElementById('fruit').value?=?result;
???}
??</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()"?/>?<!--通過?=?按鈕來調用創建的函數,得到結果-->?
???<input?type='text'?id='fruit'?/>
2016-11-15
第三行第四行的 parseInt打錯了 ,是Int不是lnt;
2016-11-15
樓上說的也對 是int 不是Lnt 單詞Parse是分析的意思,int代表整數型數值,合起來就是分析整數。在C和C++語言里,整型變量的定義用的就是int x,y,z;
2016-11-15
你賦值寫錯了 不能寫在default里,Switch的用法是變量select不為“加減乘除”才會執行default里的語句。而<body>里的是選擇框,必為加減乘除之一,附正確代碼,你看看。
<!DOCTYPE html>
<html>
?<head>
? <title> 事件</title> ?
? <script type="text/javascript">
?? function count(){
????? ?
??? //獲取第一個輸入框的值
??? var x = parseInt(document.getElementById("txt1").value);
?? ?//獲取第二個輸入框的值
??? var y = parseInt(document.getElementById("txt2").value);
?? ?//獲取選擇框的值
??? var z = document.getElementById("select").value;
?? ?//獲取通過下拉框來選擇的值來改變加減乘除的運算法則
??????? switch(z){
??????? case "+" :
??????????? //設置結果輸入框的值
??????????? document.getElementById("fruit").value =x + y;
??????????? break;
??????? case "-" :
??????????? document.getElementById("fruit").value = x - y;
??????????? break;
??????? case "*" :
??????????? document.getElementById("fruit").value = x * y;
??????????? break;
??????? case "/" :
??????????? document.getElementById("fruit").value = x / y;
??????????? break;
??????? default :
??????????? document.write("輸入有誤,請重新輸入!");
??? }
?? }
? </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()" /> <!--通過 = 按鈕來調用創建的函數,得到結果-->
?? <input type='text' id='fruit' />? ?
?</body>
</html>