dongjinwang
dongjinwang
2015-07-30 07:57
采纳率: 16.7%
浏览 1.4k

JS简单计算器问题 不知道哪错了 求大神指点



简易计算
<br> function jiSuan(){<br> var num1 = document.getElementById(&quot;num1&quot;).value*1;<br> var num2 = document.getElementById(&quot;num2&quot;).value*1;<br> var oper = document.getElementById(&quot;oper&quot;).value;<br> res = 0<br> if(oper == &quot;+&quot;){<br> res = num1 + num2<br> }else if(oper == &quot;-&quot;){<br> res = num1 - num2<br> }else if(oper == &quot;*&quot;){<br> res = num1 * num2<br> }else if(oper == &quot;/&quot;){<br> res = num1 / num2<br> }else if(oper == &quot;%&quot;){<br> res = num1 % num2<br> }else{<br> res = &quot;不能识别&quot;<br> } <br> document.getElementById(&quot;res&quot;).value = res</p> <pre><code> } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;input id=&quot;num1&quot; type=&quot;text&quot;/&gt; &lt;input id=&quot;oper&quot; type=&quot;text&quot;/&gt; &lt;input id=&quot;num2&quot; type=&quot;text&quot;/&gt; &lt;input type=&quot;button&quot; value=&quot;=&quot; onclick=&quot;jiSuan()&quot;/&gt; &lt;input id=&quot;res&quot; type=&quot;text&quot;/&gt; &lt;/body&gt; </code></pre> <p></html><br> 不知道哪里错了 谁能告诉我</p>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • danielinbiti
    danielinbiti 2015-07-30 08:04
    已采纳
      res = 0
        改成
         var res = 0;//因为你有一个dom节点 input id='res’; 两个冲突了,ie中,没声明var默认以为是这个input了。
    
    
    点赞 评论
  • dongjinwang
    dongjinwang 2015-07-30 08:11



    简易计算器二号
    <br> function jiSuan(){<br> var num1 = document.getElementById(&quot;num1&quot;).value*1;<br> var num2 = document.getElementById(&quot;num2&quot;).value*1;<br> var oper = document.getElementById(&quot;oper&quot;).value;<br> res = 0<br> if(oper == &quot;+&quot;){<br> res = num1 + num2<br> }else<br><br> if(oper == &quot;-&quot;){<br> res = num1 - num2 <br> }else<br> if(oper == &quot;*&quot;){<br> res = num1 * num2<br> }else<br> if(oper == &quot;/&quot;){<br> if(num2 == 0){<br> res = &quot;除数不能为0&quot;<br> }else{<br> res = num1 / num2<br> }<br> }else<br> if(oper == &quot;%&quot;){<br> res = num1 % num2<br> }else{<br> res = &quot;不能识别&quot;<br> } document.getElementById(&quot;res&quot;).value = res<br> }<br>








    点赞 评论

相关推荐