刘汝豪
2019-03-05 21:07
<!DOCTYPE html>
<html>
<head>
<title>简单计算器</title>
<meta charset="utf-8">
<script type="text/javascript">
function sum()
{
var select,text1,text2,text3;
text1=parseInt(document.getElementById('text1').value);
text2=parseInt(document.getElementById('text2').value);
select=parseInt(document.getElementById('select').value);
switch(select)
{
case "+": text3=text1+text2;break;
case "-": text3=text1-text2;break;
case "*": text3=text1*text2;break;
case "/": text3=text1/text2;break;
}
document.getElementById('furit').value=text3;
}
</script>
</head>
<body>
<input type="text" id="text1">
<select id="select">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="text" id="text2">
<input type="button" value="=" onclick="sum()">
<input type="text" id="furit">
</body>
</html>
获取通过下拉框来选择的值不是整数,所以取值是不要用parseInt,直接写成select=document.getElementById('select').value; 就可以运行了
JavaScript进阶篇
468061 学习 · 21891 问题
相似问题