我添加了個清空數據的按鈕和他的點擊事件 為啥沒反應
?function count(){
? ? //獲取第一個輸入框的值
? ? var num1=document.getElementById("txt1").value;
//獲取第二個輸入框的值
var num2=document.getElementById("txt2").value;
//獲取選擇框的值
var sund=document.getElementById("select").value;
//獲取通過下拉框來選擇的值來改變加減乘除的運算法則
var result="";
swith(sund)
{
? ?case "+":result=parseInt(num1)+parseInt(num2);
? ?break;
? ?case "-":result=parseInt(num1)-parseInt(num2);
? ?break;
? ?case "*":result=parseInt(num1)*parseInt(num2);
? ?break;
? ?case "/":result=parseInt(num1)/parseInt(num2);
? ?break;
? ?default:
}
? ? //設置結果輸入框的值?
? ? document.getElementById("fruit").value=result;
? ?}
? //清空函數
? ?function clearAll(){
? ? ? ?document.getElementById("txt1").value="";
? ? ? ?document.getElementById("txt2").value="";
? ? ? ?document.getElementById("fruit").value="";
? ?}
? </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' />
? ?<input type="button" value="清除數據" onclick="clearAll()"/>
?</body>
</html>
2018-08-04
swith(sund)
{
? ?case "+":result=parseInt(num1)+parseInt(num2);
? ?break;
? ?case "-":result=parseInt(num1)-parseInt(num2);
? ?break;
? ?case "*":result=parseInt(num1)*parseInt(num2);
? ?break;
? ?case "/":result=parseInt(num1)/parseInt(num2);
? ?break;
? ?default:
}
上面的switch關鍵字寫錯了