为什么不能显示结果呢

来源:6-11 编程练习

站在代码的肩膀上

2016-11-03 19:41

<!DOCTYPE html>

<html>

 <head>

  <title> 事件</title>  

  <script type="text/javascript">

   function count(){

var a=parseInt(document.getElementById("txt1").value);


var b=parseInt(document.getElementById("txt2").value);


var c=document.getElementById("select").value;


var d;


    switch(c)


    {

        case"+":

        d=a+b;

        break;

        case"-":

        d=a-b;

        break;

    }


    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>


写回答 关注

3回答

  • 俯仰2015
    2016-11-07 22:05:25
    已采纳

    在document.getElementById("fruit").value=d;和</script> 之间,你多写了一个右大括号“}”。去掉就可以了。

  • 月薪五位数
    2016-11-05 12:40:58

    能显示啊。你去复制代码到编辑器里面看一下。

  • 月薪五位数
    2016-11-03 20:38:39

    function count(){

    var txt1 = parseFloat(document.getElementById('txt1').value);

    var txt2 = parseFloat(document.getElementById('txt2').value);

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

    var fruit = document.getElementById('fruit');

    switch(select){

    case '+':

    var res = txt1 + txt2;

    break;

    case '-':

    res = txt1 - txt2;

    break;

    case '*':

    res = txt1 * txt2;

    break;

    case '/':

    res = txt1 / txt2;

    break;

    }

    fruit.value = res;

    }


    站在代码的肩...

    一样的呀

    2016-11-04 11:56:16

    共 1 条回复 >

JavaScript进阶篇

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

467395 学习 · 21877 问题

查看课程

相似问题