Achieve a simple calculator

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<input type="text" id="num1">
<select id="info">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
<option value="%">%</option>
</select>
<input type="text" id="num2">
<input type="button" value="=" id="btn">
<input type="text" id="res">
<script>
btn.onclick = function () {
was VAL1 = num1.value;
was VAL2 = num2.value;
was val3 = info.value;
switch (val3) {
case "+": res.value = parseInt(val1) + parseInt(val2); break;
case "-": res.value = val1 - val2; break;
case "*": res.value = val1 * val2; break;
case "/": res.value = val1 / val2; break;
case "%": res.value = val1 % val2; break;
}
}
</script>
</body>
</html>
 

 

 

Guess you like

Origin www.cnblogs.com/lxz123/p/11432189.html