当前位置: 代码迷 >> Java Web开发 >> 简单计算器(html+js),该怎么解决
  详细解决方案

简单计算器(html+js),该怎么解决

热度:2054   发布时间:2013-02-25 21:19:28.0
简单计算器(html+js)
<html>

<head>
<title>简单计算器</title>
<h1 align='center'>简单计算器</h1>
<script language="javascript">
function counter() {
var firstNum = parseInt(document.getElementById("firstNum").value);
var lastNum = parseInt(document.getElementById("lastNum").value);
var result = 0;
var rule = document.getElementById("rule").value;
switch(rule) {
case "+" :
result = firstNum + lastNum;
break;
case "-" :
result = firstNum - lastNum;
break;
case "*" :
result = firstNum * lastNum;
break;
case "/" :
result = firstNum / lastNum;
break;
}
document.getElementById("result").value = result;
}

</script>
</head>

<body>
<p align='center'>
<input type="text" name="firstNum" id="firstNum">

<select name="rule" id="rule">
<option value="+" id="+">+</option>
<option value="-" id="-">-</option>
<option value="*" id="*">*</option>
<option value="/" id="/">/</option>
</select>

<input type="text" name="lastNum" id="lastNum">

<input type="button" value="=" onclick="counter()">

<input type="text" name="result" id="result">
</p>
<p align="left">
<img src="logoo.jpg">
<img src="logo.jpg">
</p>
</body>

</html>


------解决方案--------------------------------------------------------
呵呵,你的除数为0的情况不作处理吗?
------解决方案--------------------------------------------------------
没写form也能行?
你的document.getElementById("result").value = result;不报错?
  相关解决方案