问题:编写计算a+b=c的网页,a b c需要客户输入,计算正确弹出对话框正确,计算错误弹出对话框错误。
本人已实现了如下页面效果
但我点击“检验是否正确”时页面没有反应,没有达到预期的效果,我的源码如下:
<html>
<head>
  <title>简单的计算器</title>
  <script language="javasript">
<!--
 function aa()
 {
    var num1=(math.n1.value)*1;
    var num2=(math.n1.value)*1;
    var num3;
    if (math.d1.selectedIndex==0) num3=num1+num2; 
    if (math.d1.selectedIndex==1) num3=num1-num2; 
    if (math.d1.selectedIndex==2) num3=num1*num2;
    if (math.d1.selectedIndex==3) num3=num1/num2;
    alert ("结果正确:"+num3);
    else
    alert("结果错误:"return);      
}
-->
</script>
</head>
<body>
<form method="post" name="math" consubmit="aa()">
<input name="n1" type="text" size="10" maxlength="10"></input>
<select name="d1" size="1">
<option value="0" selected="selected">+</option>
<option value="1" >-</option>
<option value="2" >*</option>
<option value="3" >/</option>
</select>
<input name="n2" type="text" size="10" maxlength="10">
=
<input name="n3" type="text" size="10" maxlength="10"></input>
<input name="b1" type="submit" value="检验是否正确" />
</form>
</body>
</html>出现了什么错误呢?跪求各位大侠指点出来

解决方案 »

  1.   


    <html>
    <head>
      <title>简单的计算器</title>
    <script>
         function aa()
     {  var num1=math.n1.value*1;
      var num2=math.n2.value*1;
      var num3;
      if (math.d1.selectedIndex==0) num3=num1+num2; 
      else if (math.d1.selectedIndex==1) num3=num1-num2; 
      else if (math.d1.selectedIndex==2) num3=num1*num2;
      else if (math.d1.selectedIndex==3) num3=num1/num2;
      
      if(isNaN(num3)){
        alert ("出错");
      }
      else{
        alert ("结果正确:"+num3);
        math.n3.value=num3;
      }
      return false;
    }
    </script>
    </head>
    <body>
    <form method="post" name="math" onsubmit="return aa()">
    <input name="n1" type="text" size="10" maxlength="10"></input>
    <select name="d1" size="1">
    <option value="0" selected="selected">+</option>
    <option value="1" >-</option>
    <option value="2" >*</option>
    <option value="3" >/</option>
    </select>
    <input name="n2" type="text" size="10" maxlength="10">
    =
    <input name="n3"  type="text" size="10" maxlength="10"></input>
    <input name="b1" type="submit" value="检验是否正确" />
    </form>
    </body>
    </html>错了很多 。改了些 。
      

  2.   

    <html>
    <head>
      <title>简单的计算器</title>
      <script type="text/javascript"> function aa()
     {
    //验证输入的是数字
    var  n1 = math.n1.value;
    var n2 = math.n2.value;
    var n3 = math.n3.value;
    var reg=/[0-9]+/;
    if(!n1.match(reg)||!n2.match(reg)||!n3.match(reg)){
    alert("请输入合法的数字");
    return;
    }  var num1=parseInt(math.n1.value);
      var num2=parseInt(math.n2.value);
      var num3 = parseInt(math.n3.value);
      var result;
      if (math.d1.selectedIndex=='0') result=num1+num2;  
      if (math.d1.selectedIndex=='1') result=num1-num2;  
      if (math.d1.selectedIndex=='2') result=num1*num2;
      if (math.d1.selectedIndex=='3') result=num1/num2;
       if(result==num3)
      alert ("结果正确:"+result);
      else
      alert("结果错误,正确结果是"+result);   
    }</script>
    </head>
    <body>
    <form method="post" name="math" onsubmit="aa()">
    <input name="n1" type="text" size="10" maxlength="10"></input>
    <select name="d1" size="1">
    <option value="0" selected="selected">+</option>
    <option value="1" >-</option>
    <option value="2" >*</option>
    <option value="3" >/</option>
    </select>
    <input name="n2" type="text" size="10" maxlength="10">
    =
    <input name="n3" type="text" size="10" maxlength="10"></input>
    <input name="b1" type="submit" value="检验是否正确" />
    </form>
    </body>
    </html>
      

  3.   


    <html>
    <head>
      <title>简单的计算器</title>
      <script type="text/javascript"> function aa()
     {
    //验证输入的是数字
    var  n1 = math.n1.value;
    var n2 = math.n2.value;
    var n3 = math.n3.value;
    var reg=/[0-9]+/;
    if(!n1.match(reg)||!n2.match(reg)||!n3.match(reg)){
    alert("请输入合法的数字");
    return;
    }  var num1=parseInt(math.n1.value);
      var num2=parseInt(math.n2.value);
      var num3 = parseInt(math.n3.value);
      var result;
      if (math.d1.selectedIndex=='0') result=num1+num2;  
      if (math.d1.selectedIndex=='1') result=num1-num2;  
      if (math.d1.selectedIndex=='2') result=num1*num2;
      if (math.d1.selectedIndex=='3') result=num1/num2;
       if(result==num3)
      alert ("结果正确:"+result);
      else
      alert("结果错误,正确结果是"+result);   
    }</script>
    </head>
    <body>
    <form method="post" name="math" onsubmit="aa()">
    <input name="n1" type="text" size="10" maxlength="10"></input>
    <select name="d1" size="1">
    <option value="0" selected="selected">+</option>
    <option value="1" >-</option>
    <option value="2" >*</option>
    <option value="3" >/</option>
    </select>
    <input name="n2" type="text" size="10" maxlength="10">
    =
    <input name="n3" type="text" size="10" maxlength="10"></input>
    <input name="b1" type="submit" value="检验是否正确" />
    </form>
    </body>
    </html>
      

  4.   

    建议楼主应该根据文本框的ID取他的值
    调试的时候应该用工具调试
    建议用firebug进行调试