田马达加斯加
2018-10-11 15:45
<!DOCTYPE html>
<html>
<head>
<title> 事件</title>
<script type="text/javascript">
function count(){
var result;
var num1=document.getElementById("txt1").value;
var num2=document.getElementById("txt2").value;
var calcul=document.getElementById("select").value;
switch(calcul){
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);
break;
}
return result;
console.log(result);
typeof(result);
//获取第一个输入框的值
//获取第二个输入框的值
//获取选择框的值
//获取通过下拉框来选择的值来改变加减乘除的运算法则
//设置结果输入框的值
var num3=document.getElementById("fruit").value;
var num3=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>
var num3=document.getElementById("fruit").value;
var num3=result;
换成:document.getElementById("fruit").value=result;试一下
注释这几行
//return result;
//var num3=document.getElementById("fruit").value;
//var num3=result;
改为
document.getElementById("fruit").value=result;
就可以了
不用返回result,去掉return result就行了。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title> 事件</title>
<script type="text/javascript">
function count(){
//获取第一个输入框的值
var a = parseInt(document.getElementById('txt1').value);
console.log(typeof (a));
//获取第二个输入框的值
var b = parseInt(document.getElementById('txt2').value);
console.log(b);
console.log(typeof (b));
//获取选择框的值
var c = document.getElementById('select').value;
console.log(c);
//获取通过下拉框来选择的值来改变加减乘除的运算法则
//设置结果输入框的值
var d = document.getElementById('fruit');
console.log(d)
if(c == '+'){
d.value = a + b;
}else if(c == '-'){
d.value = a - b;
}else if(c == '*'){
d.value = a * b;
}else{
d.value = a / b;
}
}
</script>
<script>
var aa = 1;
var bb = 2;
var cc = aa + bb;
console.log(cc)
</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=' = ' /> <!--通过 = 按钮来调用创建的函数,得到结果-->
<input type='text' id='fruit' />
<input type="button" onclick="count()" value="click">
</body>
</html>JavaScript进阶篇
469198 学习 · 22584 问题
相似问题