qq_慕圣1057880
2019-11-19 17:26
<script type="text/javascript"> function count(){ var txt1 = document.getElementById("txt1").value; var txt2 = document.getElementById("txt2").value; var select = document.getElementById("select").value; var kk =""; switch (select) { case "+": kk = txt1 + txt2; break; case "-": kk =txt1 - txt2; break; case "*": kk = txt1 * txt2' break; case "/": kk =txt1 / txt2; break; } document.getElementById("fruit").value = kk; } </script> </head> <body> <input type='text' id='txt1' /> <select id='select'> <option value='+'>+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> </select> <input type='text' id='txt2' /> <input type='button' value=' = ' onclick="count()" /> <!--通过 = 按钮来调用创建的函数,得到结果--> <input type='text' id='fruit' /> </body>
首先第15行【kk = txt1 * txt2 '】改为【kk = txt1 * txt2;】
其次如楼上所说要将获取到的表单的值由字符串类型转换为数值类型,用parseInt
从txt1和txt2中取值是要用parseInt()转换成数值 类型,如:
var
txt1 = parseInt(document.getElementById(
"txt1"
).value);
JavaScript进阶篇
468062 学习 · 21891 问题
相似问题