SoFunction
Updated on 2025-02-28

js form operation simple calculator


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:///TR/xhtml1/DTD/">
<html xmlns="http:///1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Written to newbies: js simple calculator</title>
<style type="text/css">
body{
font-size:12px;
color:#333;
}
#jsq input{/*Input box style*/
border:#ccc 1px solid;
border-right:#e2e2e2 1px solid;
border-bottom:#e2e2e2 1px solid;
height:18px;
line-height:18px;
padding:3px;
}
#jsq span{
color:#999
}
#jsq {/*Button Style*/
border:#e6e6e6 1px solid;
background-color:#e2e2e2;
width:30px;
height:24px;
text-align:center;
line-height:16px;
cursor:pointer;
margin:0 3px;
color:#999;
}
#jsq :hover{/*Button floating style*/
border:#e2e2e2 1px solid;
background-color:#f2f2f2;
color:#333;
}
</style>
<script type="text/javascript">
function imyeah(type){ //Computing function
var result=0;
num1 = Number(.); //Number() can be cast to a number, for example, "123abc" will be converted to "123"
num2 = Number(.);
if(num1=="" || num2==""){return false;} //If no calculation number is entered, no execution will be performed
switch(type){ //Judge the calculation symbol to be executed
case 0:result=num1+num2;break; //calculate "+"
case 1:result=num1-num2;break; //calculate "-"
case 2:result=num1*num2;break;
case 3:result=num1/num2;break;
case 4:result=num1%num2;break;
}
=result; //Show calculation results
}
</script>
</head>
<body>
<form name="jisuanqi" action="" method="get" />
<p> The first number:
<input type="text" size="10" name="num1" value="" />
</p>
<p> The second number:
<input type="text" size="10" name="num2" value="" />
</p>
<p> Calculation result:
<input type="text" size="10" name="jieguo" onClick="imyeah(0)" value="+" onfocus="()" /> <span>Left key"+", right key "Select Copy"</span>
</p>
<p>
<input type="button" class="btn" value="&ndash;" onClick="imyeah(1)"/> <!--Definition Button-->
<input type="button" class="btn" value="&times;" onClick="imyeah(2)"/ >
<input type="button" class="btn" value="&divide;" onClick="imyeah(3)"/>
<input type="button" class="btn" value="%" onClick="imyeah(4)"/>
</p>
</body>
</html>