麻煩大家幫忙給看一下是哪里的問題
<!DOCTYPE html>
<html>
?<head>
? <title> 事件</title>??
? <script type="text/javascript">
? ?function count(){
? ? a = document.getElementById("txt1").value;
? ? //獲取第一個輸入框的值
? ? b = document.getElementById("txt2").value;
//獲取第二個輸入框的值
c = document.getElementById("select")
//獲取選擇框的值
var sum="";
switch (d){
? ? case "+":parseInt(a)+parseInt(b);
? ? break;
? ? case "-":parseInt(a)-parseInt(b);
? ? break;
? ? case "*":parseInt(a)*parseInt(b);
? ? break;
? ? case "/":parseInt(a)/parseInt(b);
? ? break;
}
document.getElementById("fruit").value=sum;
//獲取通過下拉框來選擇的值來改變加減乘除的運算法則
? ? //設置結果輸入框的值?
? ??
? ?}
? </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=' = ' /> <!--通過 = 按鈕來調用創建的函數,得到結果-->?
? ?<input type='text' id='fruit' />? ?
?</body>
</html>
2019-09-23
可以了,謝謝哈
2019-09-23
1:c = document.getElementById("select").value;
這樣寫才能獲得select的值。
2:switch (c){}
這里的判斷條件是c不是d啊……
3:case "+":sum=parseInt(a)+parseInt(b);break;
你這里要將結果賦值給sum,不然sum一直是""。
4:<input type='button' value=' = ' onclick="count()"/>
你這里忘記添加點擊事件了。