<!DOCTYPE html>
<html>
<head>
<title> 事件</title>
<script type="text/javascript">
function count(){
var d;
var a=document.getElementById("text1").value;
//获取第一个输入框的值
var b=document.getElementById("text2").value;
var c=document.getElementById("select").value;//获取第二个输入框的值
//获取选择框的值
//获取通过下拉框来选择的值来改变加减乘除的运算法则
//设置结果输入框的值
switch(c)
{
case "+":d=parseInt(a)+parseInt(b);break;
case "-":d=parseInt(a)-parseInt(b);break;
case "*":d=parseInt(a)*parseInt(b);break;
case "/":d=parseInt(a)/parseInt(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>
<script type="text/javascript">
function conent(){
//获取第一个输入框的值
var txt1 = document.getElementById("txt1").value;
//获取第二个输入框的值
var txt2 = document.getElementById("txt2").value;
//获取选择框的值
var select = document.getElementById("select").value;
//获取通过下拉框来选择的值来改变加减乘除的运算法则
//设置结果输入框的值
var sum;
txt1=parseInt(txt1);
txt2=parseInt(txt2);
switch(select){
case "+":
sum=txt1+txt2;
break;
case "-":
sum=txt1-txt2;
break;
case "*":
sum=txt1*txt2;
break;
case "/":
sum=txt1/txt2;
break;
}
document.getElementById("fruit").value=sum;
}
</script>
function count(){ var a = parseInt(document.getElementById("txt1").value); var b = parseInt(document.getElementById("txt2").value); var char = document.getElementById("select").value; var result = 0; switch (char) { case "+": result = a + b; break; case "-": result = a - b; break; case "*": result = a * b; break; case "/": result = a / b; break; } document.getElementById("fruit").value = result;
你把a,b的那个id名字拼错了,不是text是txt
<!DOCTYPE html>
<html>
<head>
<title> 事件</title>
<script type="text/javascript">
function count(){
var d;
var a=document.getElementById("text1").value; //兄弟你把txt1拼成text1了
var b=document.getElementById("text2").value;//兄弟你把txt2拼成text2了
var c=document.getElementById("select").value;
switch(c)
{
case "+":d=parseInt(a)+parseInt(b);break;
case "-":d=parseInt(a)-parseInt(b);break;
case "*":d=parseInt(a)*parseInt(b);break;
case "/":d=parseInt(a)/parseInt(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>
<!DOCTYPE html>
<html>
<head>
<title> 事件</title>
<script type="text/javascript">
function count(){
var sum = 0;
//获取第一个输入框的值
var txt1 = document.getElementById("txt1").value;
//获取第二个输入框的值
var txt2 = document.getElementById("txt2").value;
//获取选择框的值
var select = document.getElementById("select").value;
//获取通过下拉框来选择的值来改变加减乘除的运算法则
//设置结果输入框的值
txt1 = parseInt(txt1);
txt2 = parseInt(txt2);
console.log(select);
switch(select){
case "*" :
sum = txt1 * txt2;
break;
case "+" :
sum = txt1 + txt2;
break;
case "-" :
sum = txt1 - txt2;
break;
case "/" :
sum = txt1 / txt2;
break;
}
console.log(sum);
document.getElementById("fruit").value = sum;
}
</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>
<!DOCTYPE html>
<html>
<head>
<title> 事件</title>
<script type="text/javascript">
function count(){
//获取第一个输入框的值
txt1=document.getElementById("txt1").value;
//获取第二个输入框的值
txt2=document.getElementById("txt2").value;
//获取选择框的值
select=document.getElementById("select").value;
//获取通过下拉框来选择的值来改变加减乘除的运算法则
var result;
switch(select){
case '/':
result=txt1/txt2;
break;
case '-':
result=txt1-txt2;
break;
case '*':
result=txt1*txt2;
break;
default:
result=parseInt(txt1)+parseInt(txt2);
}
//设置结果输入框的值
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>
我刚写的你参考一下吧!
<!DOCTYPE html>
<html>
<head>
<title> 事件</title>
<script type="text/javascript">
function count(){
//获取第一个输入框的值
txt1=document.getElementById("txt1").value;
//获取第二个输入框的值
txt2=document.getElementById("txt2").value;
//获取选择框的值
select=document.getElementById("select").value;
//获取通过下拉框来选择的值来改变加减乘除的运算法则
var result;
switch(select){
case '/':
result=txt1/txt2;
break;
case '-':
result=txt1-txt2;
break;
case '*':
result=txt1*txt2;
break;
default:
result=txt1+txt2;
}
//设置结果输入框的值
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>