使用JS完成一個簡單的計算器功能。實現2個輸入框中輸入整數後,點擊第三個輸入框能給出2個整數的加減乘除。

提示:獲取元素的值設置和獲取方法爲:例:
賦值:
document.getElementById(“id”).value = 1;
取值:
var = document.getElementById(“id”).value;
任務
第一步: 創建構建運算函數count()。
第二步: 獲取兩個輸入框中的值和獲取選擇框的值。
提示:document.getElementById( id名 ).value 獲取或設置 id名的值。
第三步: 獲取通過下拉框來選擇的值來改變加減乘除的運算法則。
提示:使用switch判斷運算法則。
第四步: 通過 = 按鈕來調用創建的函數,得到結果。
注意: 使用parseInt()函數可解析一個字符串,並返回一個整數。
<!DOCTYPE html>
<html>
<head>
<title> new document </title>
<script type="text/javascript">
function count(){
var txt1 = parseInt( document.getElementById('txt1').value);//獲取第一個輸入框的值
var txt2 = parseInt( document.getElementById('txt2').value);//獲取第二個輸入框的值
var select = document.getElementById('select').value;//獲取選擇框的值
var result = '';
switch (select)
{
case '+':
result = txt1 + txt2;
break;
case '-':
result = txt1 - txt2;
break;
case '*':
result = txt1 * txt2;
break;
case '/':
result = txt1 / txt2;
break;
}
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=' = ' = "count()" />
<input type='text' id='fruit' />
</body>
</html>