1. 程式人生 > >java script 指令碼的簡易計算器

java script 指令碼的簡易計算器

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>簡易計算器</title>
</head>

<body>
<table border="0" >
  <tr>
<td colspan="3"><h3>簡易計算器</h3></td>
  </tr>
  <tr>
    <td>第一個數</td>
    <td colspan="3"><input name="txtNum1" type="text"  id="txt1" size="25"/></td>
  </tr>
  <tr>
    <td>第二個數</td>
    <td colspan="3"><input name="txtNum2" type="text"  id="txt2" size="25"/></td>
  </tr>
  <tr>
    <td><input name="addButton2" type="button" id="jia" value="  +  "  onclick="jisuan('+')" /></td>
    <td><input name="subButton2" type="button" id="jian" value="  -  " onclick="jisuan('-')" /></td>
    <td><input name="mulButton2" type="button" id="cheng" value="  ×  " onclick="jisuan('*')" /></td>
    <td><input name="divButton2" type="button" id="chu" value="  ÷  "  onclick="jisuan('/')"/></td>
  </tr>
  <tr>
    <td>計算結果</td>
    <td colspan="3"><input name="txtresult" type="text"  id="txt3" size="25" /></td>
  </tr>
</table>
<script type="text/javascript">
function jisuan(x)
{  var a=Number(document.getElementById("txt1").value);
   var b=Number(document.getElementById("txt2").value);
   var c;
   switch(x)
   {  case "+": c=a+b;break;
      case "-": c=a-b;break;
	  case "*": c=a*b;break;
	  case "/":if(b!=0) c=a/b;
	  else c="除數不能為0,請重新輸入";
	  break;
	  
	   }
	  
document.getElementById("txt3").value=c;

	}
</script>
</body>