為何我輸出的是個字符串啊
<!DOCTYPE html>
<html>
<head>
? ?<title> 事件</title>
? ?<script type="text/javascript">
? ? ? ?function count(){
? ? ? ? ? ?var A=document.getElementById("txt1").value;
? ? ? ? ? ?var B=document.getElementById("txt2").value;
? ? ? ? ? ?var FH=document.getElementById("select").value;
? ? ? ? ? ?var res="";
? ? ? ? ? ?switch(FH){
? ? ? ? ? ? ? ?case "+":
? ? ? ? ? ? ? ? ? ?res=A+B;
? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ?case "-":
? ? ? ? ? ? ? ? ? ?res=A-B;
? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ?case "*":
? ? ? ? ? ? ? ? ? ?res=A*B;
? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ?default:
? ? ? ? ? ? ? ? ? ?res=A/B;
? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ?}
? ? ? ? ? ?document.getElementById("fruit").value=res;
? ? ? ?}
? ?</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' />
<p>為何我輸出的是個字符串啊</p>
</body>
</html>
2017-03-01
注意:?使用parseInt()函數可解析一個字符串,并返回一個整數。
parseInt(document.getElementById("txt1").value);
parseInt(document.getElementById("txt2").value);