cireay
2016-04-15 09:27
我想知道这个用if else怎么做
<script type="text/javascript">
function count(){
//获取第一个输入框的值
var number1=document.getElementById("txt1").value;
//获取第二个输入框的值
var number2=document.getElementById("txt2").value;
//获取选择框的值
var option=document.getElementById("select").value;
//获取通过下拉框来选择的值来改变加减乘除的运算法则
if(option=="+")
sum=parseInt(number1)+parseInt(number2);
else
if(option=="-")
sum=parseInt(number1)-parseInt(number2);
else
if(option=="*")
sum=parseInt(number1)*parseInt(number2);
else
sum=parseInt(number1)/parseInt(number2);
//设置结果输入框的值
document.getElementById("fruit").value=sum;
}
</script>
function count() {
var txt1 = parseInt(document.getElementById('txt1').value); //获取第一个输入框的值
var txt2 = parseInt(document.getElementById('txt2').value); //获取第二个输入框的值
var sel = document.getElementById('select').value; //获取选择框的值
var result = '';
if(sel = '+') {
result = txt1 + txt2;
} else if(sel = '-') {
result = txt1 - txt2;
} else if(sel = '*'){
result = txt1 * txt2;
}
else{
result = txt1 / txt2;
}
document.getElementById('fruit').value = result;
}
if (select == "+")
{
sum = parseFloat(num1) + parseFloat(num2);
}
else if (select == "-")
{
sum = parseFloat(num1) - parseFloat(num2);
}
else if (select == "*")
{
sum = parseFloat(num1) * parseFloat(num2);
}
else if (select == "/")
{
sum = parseFloat(num1) / parseFloat(num2);
}
else
{
document.write("default!!");
}
var txt1 = parseInt( document.getElementById('txt1').value);//获取第一个输入框的值
var txt2 = parseInt( document.getElementById('txt2').value);//获取第二个输入框的值
var select = document.getElementById('select').value;//获取选择框的值
var result = '';
if (select=="+")
{
result = txt1 + txt2;
break;}
else if(select=="-")
{result = txt1 - txt2;
break;}
else if(select=="*")
{result = txt1 * txt2;
break;}
else
{result = txt1 / txt2;
break;}
document.getElementById('fruit').value = result;//设置结果输入框的值
}
JavaScript进阶篇
468061 学习 · 21891 问题
相似问题