慕哥0503841
2019-05-07 14:11
switch(oSlt) {
case "+":
result = parseFloat(oTxt1) + parseFloat(oTxt2);
break;
}
<script type="text/javascript">
function count(){
var sum=0;
//获取第一个输入框的值
var num1 = document.getElementById("txt1").value;
//获取第二个输入框的值
var num2 = document.getElementById("txt2").value;
//获取选择框的值
var sel = document.getElementById("select").value;
//获取通过下拉框来选择的值来改变加减乘除的运算法则
switch(sel)
{
case '+':sum=parseInt(num1)+parseInt(num2);break;
case '-':sum=parseInt(num1)-parseInt(num2);break;
case '*':sum=parseInt(num1)*parseInt(num2);break;
case '/':sum=parseInt(num1)/parseInt(num2);break;
}
//设置结果输入框的值
var result = document.getElementById("fruit");
result.value=sum;
}
</script>
parseFloat(oTxt2)是把文本转换为数字,
例如:
结果:
parseFloat(oTxt1)+ parseFloat(oTxt2)=5;
parseFloat(oTxt1+ oTxt2)=41;
JavaScript进阶篇
468194 学习 · 21891 问题
相似问题