javaScript实现-在两个文本框中输入值计算加减乘除法结果显示在另一个文本框中
在两个文本框中输入值计算加减乘除法结果显示在另一个文本框中。<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" con
·
在两个文本框中输入值计算加减乘除法结果显示在另一个文本框中。
方法一:用if - else
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input id="n1">
<select id="flag">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input id="n2">
<button onclick="sum()">=</button>
<input id="n3" >
<script>
//取下拉框的选中项的value
function sum()
{
var one = document.getElementById("n1");
var two = document.getElementById("n2");
var three = document.getElementById("n3");
//判断传入的符号
if(flag.value == '+'){
three.value= parseInt(one.value)+parseInt(two.value);//将计算的结果显示在另一个文本框内
}else if(flag.value == '-'){
three.value= parseInt(one.value)-parseInt(two.value);
}else if(flag.value == '*'){
three.value= parseInt(one.value)*parseInt(two.value);
}else if(flag.value == '/'){
if(two.value==0){
alert("除数不能为0!!!");
}else{
three.value= parseInt(one.value)/parseInt(two.value);
}
}
}
</script>
</body>
</html>
方法二:动态的获取
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>用文本框输入数字计算加减乘除法结果显示在另一个文本框</title>
</head>
<body>
<input id="n1">
<select id="flag">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input id="n2">
<button onclick="test()">=</button>
<input id="n3">
<script>
//取下拉框的选中项的value
function test(){
//取输出的值
var n1 = document.getElementById("n1").value;
var n2 = document.getElementById("n2").value;
//取下拉框架选中的value
var flag = document.getElementById("flag").value;
var result = "var a = parseInt(n1);var b = parseInt(n2); return a"+flag+"b";
var fun = new Function("n1","n2",result);
var n3 = fun(n1,n2);
document.getElementById("n3").value = n3;
}
</script>
</body>
</html>
方法三:用eval()
eval(字符串) 将参数字符串做为一个javascript表达式进行执行,用它可以生成一些动态的javascript表达式。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input id="n1" value="" />
<select id="flag">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input id="n2" value="" />
<button onclick="test()">=</button>
<input id="n3" />
<script>
function test(){
var n1=parseFloat(document.getElementById("n1").value);
var n2=parseFloat(document.getElementById("n2").value);
var flag=document.getElementById("flag").value;
var result=eval(parseFloat(n1)+flag+parseFloat(n2));
document.getElementById('n3').value=result;
}
</script>
</body>
</html>
结果页面
加法:
减法:
乘法:
除法:
更多推荐
已为社区贡献2条内容
所有评论(0)