日幕
2019-01-26 09:31
<html> | |
<head> | |
<title> new document </title> | |
<script type="text/javascript"> | |
function count(){ | |
var txt1 = parseInt( document.getElementById('txt1').value);//获取第一个输入框的值 | |
var txt2 = parseInt( document.getElementById('txt2').value);//获取第二个输入框的值 | |
var select = document.getElementById('select').value;//获取选择框的值 | |
var result = ''; | |
switch (select) | |
{ | |
case '+': | |
result = txt1 + txt2; | |
break; | |
case '-': | |
result = txt1 - txt2; | |
break; | |
case '*': | |
result = txt1 * txt2; | |
break; | |
case '/': | |
result = txt1 / txt2; | |
break; | |
} | |
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> |
function count(){ //获取第一个输入框的值 var num1=parseInt(document.getElementById("txt1").value); //获取第二个输入框的值 var num2=parseInt(document.getElementById("txt2").value); //获取选择框的值 var oper=document.getElementById("select").value; //获取通过下拉框来选择的值来改变加减乘除的运算法则 var res; switch (oper){ case '+':res=num1+num2; break; case '-':res=num1-num2;break; case '*':res=num1*num2;break; case '/':res=1.0*num1/num2; break; defalut:break; } //设置结果输入框的值 document.getElementById("fruit").value=res; } function count2() { document.getElementById("fruit").value = eval(document.getElementById("txt1").value + document.getElementById("select").value + document.getElementById("txt2").value); }
JavaScript进阶篇
468064 学习 · 21891 问题
相似问题