參考下正確答案
<!DOCTYPE html>
<html>
?<head>
? <title> 事件</title>??
? <script type="text/javascript">
? ?function count(){
? ? //獲取第一個輸入框的值
? ? var txt1 = parseInt(document.getElementById("txt1").value);
//獲取第二個輸入框的值
var txt2 = parseInt(document.getElementById("txt2").value);
//獲取選擇框的值
var sel = document.getElementById("select").value;
//獲取通過下拉框來選擇的值來改變加減乘除的運算法則
switch(sel){
? ? case "+":
? ? ? ? var result = txt1 + txt2;
? ? ? ? break;
? ? case "-":
? ? ? ? var result = txt1 - txt2;
? ? ? ? break;
? ? case "*":
? ? ? ? var result = txt1*txt2;
? ? ? ? break;
? ? case "/":
? ? ? ? var result = txt1/txt2;
? ? ? ? break;
}
? //設置結(jié)果輸入框的值
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ù),得到結(jié)果-->?
? ?<input type='text' id='fruit' />? ?
?</body>
</html>
2018-07-03
//獲取通過下拉框來選擇的值來改變加減乘除的運算法則
這部的加算法你這樣寫直接就錯誤的,因為不加任何函數(shù),那么你這里的算法比如“1+1”結(jié)果是11,是字符串合并并不是數(shù)學運算,如果要實現(xiàn)運算,要用parseInt進行強制雷翔轉(zhuǎn)換,如果你要輸入的數(shù)字中包含小數(shù),那么要使用parseFloat進行運算
那么問題來了,你的正確答案還正確嗎?
2018-07-03
點贊?