<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>简单的计算器</title>
<script type="text/javascript">
function Cal(){
var num1,num2,op,num;
num1=form.num1.value;
num2=form.num2.value;
op=form.op.value;
if(num1==""){
alert("第一个数不能为空");
return false;
}
if(num2==""){
alert("第二个数不能为空");
return false;
}if(op=="/"){
if(num==0){
alert("除数不能为0");
return false;
}
}
num1=parseInt(num1);
num2=parseInt(num2);
switch(op){
case"+":
num=num1+num2;
break;
case"-":
num=num1-num2;
break;
case"/":
num=num1/num2;
break;
case"*":
num=num1*num2;
break;
}
alert("计算后的结果是:"num);
}
</script>
</head>
<body>
<form name="form">
<input type="text" size="10" name="num1">
<select name="op">
<option value="+">+</option>
<option value="-">-</option>
<option value="/">/</option>
<option value="*">*</option>
</select>
<input type="text" size="10" name="num2" />
<input type="button" value="JS" name="but" onclick="Cal()"/>
</form>
</body>
</html>