问答详情
源自:6-11 编程练习

请问我的代码哪里出错了?为什么没有出现加减乘除的效果。

function count(){
    var num1=document.getElementById("txt1").valne;
   var num2=document.getElementById("txt2").value;
   var sel=document.getElementById("select").value;
    var result=document.getElementById("fruit").value;
   switch(sel){
     case "+":
         result=parseInt(num1)+parseInt(num2);
         break;
     case "-":
         result=parseInt(num1)-parseInt(num2);
         break;
     case "*":
         result=parseInt(num1)*parseInt(num2);
         break;
     case "/":
         result=parseInt(num1)/parseInt(num2);
   }
   }

提问者:慕设计3388631 2016-07-04 16:48

个回答

  • chri
    2016-07-04 17:26:57
    已采纳

        <script type="text/javascript">
            function count() {

                //获取第一个输入框的值
                var para1 = parseInt(document.getElementById("txt1").value);
                //获取第二个输入框的值
                var para2 = parseInt(document.getElementById("txt2").value);
                //获取选择框的值
                var sign = document.getElementById("select").value;
                //获取通过下拉框来选择的值来改变加减乘除的运算法则
                var result;
                switch (sign) {
                    case '+':
                        result = para1 + para2;
                        break;
                    case '-':
                        result = para1 - para2;
                        break;
                    case '*':
                        result = para1 * para2;
                        break;
                    case '/':
                        result = para1 / para2;
                        break;
                }
                //设置结果输入框的值
                document.getElementById("fruit").value = result;
            }
        </script>

  • 幻灭丶
    2016-07-04 17:08:28

    第一行 var num1=document.getElementById("txt1").valne;      valne改为value