head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script>   
  function   math()
  {   
  a=eval(avalue.value);   
  b=eval(bvalue.value);     
  alert("x="+eval(yvalue.value));
 
  }
     
  </script>     
   
</head>
<body>
<div>
x=<input   id=xvalue   value="a+b"></input><br>   
a=<input   id=avalue   value=1></input><br>   
b=<input   id=bvalue   value=1.5></input><br> 
<input   type=button   value="计算"   onclick="math()"></input> <br >
</div>
<div>
x=<input   id=xvalue   value="a-b"></input><br>   
a=<input   id=avalue   value=1></input><br>   
b=<input   id=bvalue   value=1.5></input><br> 
<input   type=button   value="计算"   onclick="math()"></input> <br >
</div></body>
望高手替我解决下,怎么同时实现加减法。看看哪错了,我是新手!

解决方案 »

  1.   


    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
    <title>无标题文档 </title> 
    <script>  
      function  math(index) 
      {  
      a=parseFloat(document.getElementById("avalue"+index).value);  
      b=parseFloat(document.getElementById("bvalue"+index).value);    
      alert("x="+eval(document.getElementById("xvalue"+index).value)); 
      } 
        
      </script>    
      
    </head> 
    <body> 
    <div> 
    x= <input  id=xvalue1  value="a+b"> </input> <br>  
    a= <input  id=avalue1  value=1> </input><br>
    b= <input  id=bvalue1  value=1.5> </input><br>
    <input  type=button  value="计算"  onclick="math(1)"> </input> <br > 
    </div> 
    <div> 
    x= <input  id=xvalue2  value="a-b"> </input> <br>  
    a= <input  id=avalue2  value=1> </input> <br>  
    b= <input  id=bvalue2  value=1.5> </input> <br> 
    <input  type=button  value="计算"  onclick="math(2)"> </input> <br > 
    </div> </body> 
      

  2.   


    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档 </title>
    <script> 
    function  mathadd()

    var a=document.getElementById('avalue').value; 
    var b=document.getElementById('bvalue').value;
    var oper = document.getElementById('xvalue').value;
    r = oper.replace(/a/, a);
    r = r.replace(/b/, b);
    alert(r+"="+eval(r));
    }
    function  mathplus()

    var a=document.getElementById('cvalue').value; 
    var b=document.getElementById('dvalue').value;
    var oper = document.getElementById('yvalue').value;
    r = oper.replace(/a/, a);
    r = r.replace(/b/, b);
    alert(r+"="+eval(r));
    }
    </script>
    </head>
    <body>
    <div>
    x= <input  id=xvalue  value="a+b"> </input> <br> 
    a= <input  id=avalue  value=1> </input> <br> 
    b= <input  id=bvalue  value=1.5> </input> <br>
    <input  type=button  value="计算"  onclick="mathadd()"> </input> <br >
    </div>
    <div>
    x= <input  id=yvalue  value="a-b"> </input> <br> 
    a= <input  id=cvalue  value=1> </input> <br> 
    b= <input  id=dvalue  value=1.5> </input> <br>
    <input  type=button  value="计算"  onclick="mathplus()"> </input> <br >
    </div></body>
    </html>