<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>计算器</title>
<script type="text/javascript">
window.onload=function(){
var sum1= document.getElementById("input01");
var sum2= document.getElementById("input02");
var operator= document.getElementById("select").value;
var oBtn = document.getElementById('btn');
oBtn.onclick = function(){
var val01 = sum1.value;
var val02 = sum2.value;
if(val01=="" || val02==""){
alert('输入框不能为空!');
return;
}
if(isNaN(val01) || isNaN(val02)){
alert('请输入数字!');
return;
}
switch(operator){
case'0':
alert(parseFloat(val01)+parseFloat(val02));
break;
case'1':
alert(parseFloat(val01)-parseFloat(val02));
break;
case'2':
alert(parseFloat(val01)*parseFloat(val02));
break;
case'3':
alert(parseFloat(val01)/parseFloat(val02));
break;
}
}
}
</script>
</head>
<body>
<h1>计算器</h1>
<input type="text" name="" id="input01" />
<select id="select">
<option value="0">+</option>
<option value="1">-</option>
<option value="2">*</option>
<option value="3">/</option>
</select>
<input type="text" name="" id="input02" />
<input type="button" name="" value="计算" id="btn" onclick="alert" />
</body>
</html>