为了账号安全,请及时绑定邮箱和手机立即绑定

求大佬看看有啥问题

<!DOCTYPE html>

<html>

 <head>

  <title> 事件</title>  

  <script type="text/javascript">

   function count()

   {

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

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

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

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

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

//获取选择框的值

   switch(c)

   {

       case "+":

           d=a+b;

           break;

       case "-":

           d=a-b;

           break;

       case "*":

           d=a*b;

           break;

       default:

           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 回答

1. value前面没加 . 

2. 如果你alert一下a.value 你会发现他是一个string是做不能直接运算的,用parseInt() 换成int

3. d 没有声明

0 回复 有任何疑惑可以回复我~

用这个eval方法直接可以轻松搞定!!!!

<!DOCTYPE html>

<html>

 <head>

  <title> 事件</title>  

  <script type="text/javascript">

   function count(){

       

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

    var inputValue_1=document.getElementById("txt1").value

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

    var inputValue_2=document.getElementById("txt2").value

    //获取选择框的值

    var inputValue_3=document.getElementById("select").value

    console.log(inputValue_3);

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

    var result = window.eval(inputValue_1+inputValue_3+inputValue_2)

    console.log(result);

    //设置结果输入框的值 

    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>


0 回复 有任何疑惑可以回复我~
<!DOCTYPE html>	<html>	<head>	<title> 事件 </title>   	<script type = "text/javascript" >	function count() {		var a = document.getElementById("txt1").value;		var b = document.getElementById("txt2").value;		var sum = 0;		//获取第一个输入框的值		//获取第二个输入框的值		//获取选择框的值		var c = document.getElementById("select").value;		//获取通过下拉框来选择的值来改变加减乘除的运算法则		switch(c) {			case "+":				sum = parseInt(a)+ parseInt(b);				break;			case "-":				sum = parseInt(a) - parseInt(b);				break;			case "*":			    sum = parseInt(a) * parseInt(b);			    break;			case "/":			    sum =parseInt(a) / parseInt(b);			    break;		}		document.getElementById("fruit").value=sum;	}		</script>		</head> 		<body>			<input type = 'text'id = 'txt1' / >			<select id = 'select' onchange="count()">			<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>


0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
JavaScript进阶篇
  • 参与学习       467077    人
  • 解答问题       21874    个

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

进入课程

求大佬看看有啥问题

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信