用js实现简易计算器(只能实现加减乘除)
2009-10-12 11:29
267 查看
<mce:script type="text/javascript"><!-- function operation(flag) { fl = flag; switch(flag) { case "+": doAdd();break; case "-": doMinus();break; case "*": doMultiply();break; case "/": doDivide();break; } } var num1; var num2; var isPass = true; var result; var fl; function getValue() { isPass =true; num1 =document.getElementById("txtNum1").value; num2 = document.getElementById("txtNum2").value; result = document.getElementById("txtResult"); checkVal(); } function checkVal() { if(num1=="" || num2=="") { alert("不能为空"); isPass = false; }else { if(num1!=0) { if(!Number(num1)) { isPass = false; } } if(fl=="/") { if(num2 == 0) { alert("除数不能为0"); isPass = false; } } if(fl!="/"){ if(num2!=0){ if(!Number(num2)) { alert("请输入数字"); isPass = false; } } } } } //加法 function doAdd() { getValue(); if(isPass) { result.value =Number(num1)+Number(num2); } } //减法 function doMinus() { getValue(); if(isPass) { result.value =Number(num1)-Number(num2); } } //乘法 function doMultiply(){ getValue(); if(isPass) { result.value =Number(num1)*Number(num2); } } //除法 function doDivide(){ getValue(); if(isPass) { result.value =Number(num1)/Number(num2); } } function clearValue() { document.getElementById("txtNum1").value = ""; document.getElementById("txtNum2").value = ""; document.getElementById("txtResult").value = ""; } // --></mce:script>
<div style="z-index: 101; left: 134px; position: absolute; top: 55px; height: 98px"> <table style="width: 80%; height: 92px"> <tr> <td colspan="3" style="background-image: url(images/logo.gif); background-repeat: no-repeat; height: 37px; padding-left: 230px;"> 欢迎您来到淘宝!</td> </tr> <tr style="background-color: #33cccc;" mce_style="background-color: #33cccc;"> <td style="background-image: url(images/shop.gif); width: 111px; background-repeat: no-repeat; height: 61px;"> </td> <td colspan="2" style="height: 61px"> 购物简易计算器</td> </tr> <tr style="background-color: #33cccc;" mce_style="background-color: #33cccc;"> <td style="width: 111px"> 第一个数:</td> <td colspan="2"> <input id="txtNum1" type="text" /></td> </tr> <tr style="background-color: #33cccc;" mce_style="background-color: #33cccc;"> <td style="width: 111px"> 第二个数:</td> <td colspan="2"> <input id="txtNum2" type="text" /></td> </tr> <tr style="background-color: #33cccc;" mce_style="background-color: #33cccc;"> <td colspan="3" style="height: 26px"> <input id="btnAdd" style="width: 63px" type="button" value="+" onclick="operation('+');" /> <input id="btnMinus" style="width: 63px" type="button" value="-" onclick="operation('-');" /> <input id="btnMultiply" style="width: 63px" type="button" value="*" onclick="operation('*');" /> <input id="btnDivide" style="width: 63px" type="button" value="/" onclick="operation('/');" /> <input id="btnClear" type="button" style="width: 63px" value="清空" onclick="clearValue();" /></td> </tr> <tr style="background-color: #33cccc;" mce_style="background-color: #33cccc;"> <td colspan="3" style="height: 26px"> 计算结果:<input id="txtResult" readonly="readonly" type="text" /></td> </tr> </table> </div>
有什么好的建议或问题还请各位朋友指出,谢谢。
相关文章推荐
- js实现input输入框只能输入数字的功能
- JS实现的加减乘除四则运算计算器示例
- 超级简易的JS计算器实例讲解(实现加减乘除)
- JS实现精确加减乘除
- 用JS脚本实现CheckBoxList只能选择其中的两个
- js 要求实现只能输入数字小数点、其他输入不进去
- Js实现限制文本框只能输入字母或数字的方法总结
- js控制文本框只能输入中文、英文、数字与指定特殊符号的实现代码
- js实现加减乘除
- Js屏蔽按住键盘一直输入的问题,实现按住也只能输入一个字符
- js实现简易计算器
- js实现登录用户名和密码只能是数字或字母
- JS_实现一个简易计算器
- 纯JS实现只能输入数字
- 用JS脚本实现CheckBoxList只能选择其中的两个!
- 用JS脚本实现CheckBoxList只能选择其中的两个!
- 简易计算器用js实现的代码
- js实现加减乘除
- js实现input输入框只能输入数字的功能(完美测试通过)
- php与js实现确保每次表单只能提交一次