為什么這個不對
<!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 s=parseInt(document.getElementById("select").value);
?????? switch(s)
?????? {
?????????? 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;
?????? }
??? //獲取第一個輸入框的值
?//獲取第二個輸入框的值
?//獲取選擇框的值
?//獲取通過下拉框來選擇的值來改變加減乘除的運算法則
??? //設置結果輸入框的值
???
?? }
? </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>
2017-10-15
switch()函數所判斷的變量s經過parseInt函數后已經是一個整數了(parseint解析字符串返回整數值),但在此后case所匹配的值卻是字符“+-*/”,和變量類型不匹配。。所以出錯了。。。。想改正的話s不用用parseint函數,直接獲取ID值就行var s=document.getElementById("select").value;