慕粉4169323
2016-10-19 13:42
<!DOCTYPE html>
<html>
<head>
<title>事件</title>
<script type="text/javascript">
function count(){
var result;
var txt1=document.getElementById("txt1").value;
num1=parseInt(txt1);
//获取第一个输入框的值
var txt2=document.getElementById("txt2").value;
num2=parseInt(txt2);
//获取第二个输入框的值
var count=document.getElementById("select").value;
//获取选择框的值
switch(count){
case "+": result=num1+num2;break;
case "-": result=num1-num2;break;
case "*": result=num1*num2;break;
case "/": result=num1/num2;break;
}
//获取通过下拉框来选择的值来改变加减乘除的运算法则
//设置结果输入框的值
document.getElementById("fruit").value=result;
return 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>
function count()
{
var a = document.getElementById("txt1").value;
var num1 = parseInt(a);
var b = document.getElementById("txt2").value;
var num2 = parseInt(b);
var c = document.getElementById("select").value;
var d;
if(c == "+")
{
d = num1 + num2;
}
else if(c == "-")
{
d = num1 - num2;
}
else if(c == "*")
{
d = num1 * num2;
}
else
{
d = num1 / num2;
}
document.getElementById("fruit").value = d;
return d;
}
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="utf-8">
<title> 事件</title>
<script type="text/javascript">
function count(){
//获取第一个输入框的值
var x = document.getElementById('txt1').value;
//获取第二个输入框的值
var y = document.getElementById('txt2').value;
//获取选择框的值
var symbol = document.getElementById('select').value;
//获取通过下拉框来选择的值来改变加减乘除的运算法则
if (document.getElementById('select').value=="+")
{
var result = Number(x) + Number(y);
}
else if (document.getElementById('select').value=="-")
{
var result = x - y;
}
else if (document.getElementById('select').value=="*")
{
var result = x * y;
}
else
{
if(y!=0) {var result = x / y;}
else {alert("除数不能为0!");}
}
//设置结果输入框的值
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>
可以,挺不错的
JavaScript进阶篇
468190 学习 · 21891 问题
相似问题