点击=没反应,不知道问题出在哪里,求帮忙

来源:6-11 编程练习

1小时先生

2016-08-31 14:18

<!DOCTYPE html>
<html>
 <head>
  <title> 事件</title>  
  <script type="text/javascript">
   function count()
   {
    var d = "";
    var a= document.getElementById("txt1").value;
    var b= document.getElementById("txt2").value;
    var c= document.getElementById("select").value;
    switch(c)
        {
            case "+":
            d = parseInt(a)+parseInt(b);
            break;
            case "-":
            d = a-b;
            break;
            case "*":
            d = a*b;
            break;
            default:
            d = a/b;
        }
    document.getElementById({"fruit").value = d ;
   }
  </script>
 </head>
 <body>
   <input type='text' id='txt1' />
   <select id='select'>
        <option value='+'>+</option>
        <option value="-">-</option>
        <option value="*">*</option>
        <option value="/">/</option>
   </select>
   <input type='text' id='txt2' />
   <input type='button' value=' = ' onclick="count()" /> <!--通过 = 按钮来调用创建的函数,得到结果-->
   <input type='text' id='fruit' />   
 </body>
</html>

写回答 关注

2回答

  • imalwayshere
    2016-08-31 17:03:07
    function count(){
        var num1=parseFloat(document.getElementById("txt1").value);
        var num2=parseFloat(document.getElementById("txt2").value);
        var result=document.getElementById("fruit");
        var op=document.getElementById("select").value;
        switch(op){
            case'+':result.value=num1+num2;break;
            case'-':result.value=num1-num2;break;
            case'*':result.value=num1*num2;break;
            case'/':result.value=num1/num2;break;
    }

    这样是不是简洁点。

  • 1小时先生
    2016-08-31 14:31:56

    找到问题了,是代码多写了一个{,document.getElementById({"fruit").value = d ;

JavaScript进阶篇

本课程从如何插入JS代码开始,带您进入网页动态交互世界

467397 学习 · 21877 问题

查看课程

相似问题