JS 简易的计算器

一个

输出为:

代码:

 1 <!DOCTYPE html>
 2 <html>
 3 <script type="text/javascript" src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
 4 <meta charset="UTF-8">
 5 <title></title>
 6 <style type="text/css">
 7 ul li{
 8 list-style:none;
 9 padding:7px;
10 }  
11 </style>
12 <body>
13 <scripe type="text/javascript">
14 function anniu(){
15 var a=document.getElementById("k2").value;
16 var b=document.getElementById("k3").value;
17 var radio=document.getElementByName("ra");
18 
19 for(i=0;i<radio.length;i++){
20 if(radio[i].checked){    //checked选中事件
21 var fh=radio[i].value;
22 }
23 }
24 var result=0;
25 switch(fh){
26 case '+':
27 result=parseFloat(a)+parseFloat(b);
28 break;
29 case '-':
30 result=parseFloat(a)-parseFloat(b);
31 break;
32 switch(fh){
33 case '*':
34 result=parseFloat(a)*parseFloat(b);
35 break;
36 case '/':
37 result=parseFloat(a)/parseFloat(b);
38 break;
39 default:
40 alert('符号有误');
41 break;
42 }
43 document.getElementById("k1").value=result;
44 }
45 
46 </script>
47 <ul>
48 <li>结果:<input id="k1" disabled="disabled"/></li>
49 <li>第一个数:<input id="k2" type="text"/></li>
50 <li>第二个数:<input id="k3" type="text"></input></li>
51 <li id="fuhao">
52 <input value='+' type="radio" name="ra">+</input>
53 <input value='-' type="radio" name="ra">-</input>
54 <input value='*' type="radio" name="ra">*</input>
55 <input value='/' type="radio" name="ra">/</input>
56 </li>
57 <li><button type='button' onclick="anniu()">提交</button></li>
58 </ul>
70 </body>
71 </html>

猜你喜欢

转载自www.cnblogs.com/chenyuan7/p/9181048.html