感觉代码一样,但是不能计算

来源:6-11 编程练习

lxl820813

2019-06-09 17:56

<!DOCTYPE html>

<html>

 <head>

  <title> 事件</title>  

  <script type="text/javascript">

   function count(){

    

    //获取第一个输入框的值

    var txt1 = parseInt(document.getElemetById('txt1').value);   

//获取第二个输入框的值

var txt2 = parseInt(document.getElemetById('txt2').value); 

//获取选择框的值

var select = document.getElemetById('select').value; 

//获取通过下拉框来选择的值来改变加减乘除的运算法则

    //设置结果输入框的值 

    var result = '';

    switch (select) {

        case '+':

            result = txt1 + txt2;

            break;

        case '-':

            result = txt1 - txt2;

            break;

        case '*':

            result = txt1 * txt2;

            break;

        case '/':

            result = txt1 / txt2;

            break;

    }

    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>


写回答 关注

1回答

  • weixin_慕村931117
    2019-06-09 22:39:33
    已采纳

    getElementById  写代码仔细点哦 你少打了个n  你的是 getElemeltById

    还有就是这个方法括号里用的是双引号,你的事单引号

    lxl820...

    谢谢,可以了!

    2019-06-10 20:18:47

    共 1 条回复 >

JavaScript进阶篇

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

467373 学习 · 21877 问题

查看课程

相似问题