帮忙看看代码哪里有错了,不显示结果

来源:6-11 编程练习

Fitting

2015-11-30 13:10

function count(){
      
    //获取第一个输入框的值
     var otxt1 = document.getElementById("txt1").value;
 //获取第二个输入框的值
     var otxt2 = document.getElementById("txt2").value;
 //获取选择框的值
    var oselect = document.getElementById("select").value;
 //获取通过下拉框来选择的值来改变加减乘除的运算法则
    var result="";
    //设置结果输入框的值
    switch(select){
    case "+";   
    result = parseFloat(otxt1)+parseFloat(otxt2);
    break;
    case "-";
    result = parseFloat(otxt1)-parseFloat(otxt2);
    break;
    case "*";
    result = parseFloat(otxt1)*parseFloat(otxt2);
    default;
    result = parseFloat(otxt1)/parseFloat(otxt2);
    }
    document.getElementById("fruit").value = result;
   }
  </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>

写回答 关注

3回答

  • echo_kinchao
    2015-11-30 13:50:13

    你的select没有带入 你定义的是oselect

  • 魈小沐
    2015-11-30 13:30:13

    建议你使用firebug类似调试工具,很容器发现这些小问题

  • 一毛钱
    2015-11-30 13:12:29

     switch(select){ 这块写错了 ,应该是switch(oselect) 你定义的是oselect 你用的是select

JavaScript进阶篇

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

467385 学习 · 21877 问题

查看课程

相似问题