看不出問題在哪
<!DOCTYPE html>
<html>
?<head>
? <title> 事件</title> ?
? <script type="text/javascript">
? ?function count(){
? ? var htxt1=document.getElementById("txt1").value;
? ? var htxt2=document.getElementById("txt2").value;
? ? var sfruit=document.getElementById("fruit").value;?
? ? var result="";//獲取第一個輸入框的值
//獲取第二個輸入框的值
//獲取選擇框的值
switch(sfruit)
{
? case "+"
? result=parseInt(htxt1)+parseInt(htxt2);
? break;
? case "-"
? result=parseInt(htxt1)-parseInt(htxt2);
? break;
? case "*"
? result=parseInt(htxt1)*parseInt(htxt2);
? break;
? default "/"
? result=parseInt(htxt1)/parseInt(htxt2);
}
//獲取通過下拉框來選擇的值來改變加減乘除的運算法則
? ? //設置結果輸入框的值?
? ? 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()"/> <!--通過 = 按鈕來調(diào)用創(chuàng)建的函數(shù),得到結果-->?
? ?<input type='text' id='fruit' />?
? ?
?</body>
</html>
2017-04-02
? ?case "+":
? ? ? ?result=parseInt(htxt1)+parseInt(htxt2);
? ? ? ?break;
? ?case "-":
? ? ? ?result=parseInt(htxt1)-parseInt(htxt2);
? ? ? ?break;
? ?case "*":
? ? ? ?result=parseInt(htxt1)*parseInt(htxt2);
? ? ? ?break;
? ?default:
? ? ? ?result=parseInt(htxt1)/parseInt(htxt2);
}
改成這樣試試,符號錯了
2017-04-08
首先,你沒有獲取select元素,你獲取了fruit元素,弄反了;其次,你沒有冒號,在case后面