課程
/前端開發(fā)
/JavaScript
/JavaScript進(jìn)階篇
我想知道這個用if else怎么做
2016-04-15
源自:JavaScript進(jìn)階篇 6-11
正在回答
<script type="text/javascript">? function count(){?? ??? //獲取第一個輸入框的值? ?? ?var number1=document.getElementById("txt1").value;?? ?//獲取第二個輸入框的值??? var number2=document.getElementById("txt2").value;?? ?//獲取選擇框的值??? var option=document.getElementById("select").value;?? ?//獲取通過下拉框來選擇的值來改變加減乘除的運(yùn)算法則?? ?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);?? ??? ??? ??? ?//設(shè)置結(jié)果輸入框的值 ?? ?document.getElementById("fruit").value=sum;?? ??? ??? ??? ?? }? </script>
cireay 提問者
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;//獲取選擇框的值
if (select=="+")
{
break;}
else if(select=="-")
{result = txt1 - txt2;
else if(select=="*")
? ? ? ? ? ? ? ? {result = txt1 * txt2;
else
{result = txt1 / txt2;
break;} ?
? ? ? ? ?document.getElementById('fruit').value = result;//設(shè)置結(jié)果輸入框的值?
? ?}
舉報
本課程從如何插入JS代碼開始,帶您進(jìn)入網(wǎng)頁動態(tài)交互世界
3 回答我這道題好難,想半天都不知道怎么做?你們知道嗎?
2 回答做一個計算器的練習(xí)題,用if...else做可以么?
4 回答我想用else if做,為什么我輸入沒有反應(yīng)呢,愿大佬指教
1 回答這個if...else嵌套語句用switch的話,應(yīng)該怎么寫?
3 回答我想知道這個返回back()有什么用?本地測試也沒用
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2016-04-15
<script type="text/javascript">
? function count(){
?? ??? //獲取第一個輸入框的值
? ?? ?var number1=document.getElementById("txt1").value;
?? ?//獲取第二個輸入框的值
??? var number2=document.getElementById("txt2").value;
?? ?//獲取選擇框的值
??? var option=document.getElementById("select").value;
?? ?//獲取通過下拉框來選擇的值來改變加減乘除的運(yùn)算法則
?? ?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);
?? ??? ??? ??? ?//設(shè)置結(jié)果輸入框的值
?? ?document.getElementById("fruit").value=sum;
?? ??? ??? ??? ?
? }
? </script>
2017-04-21
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;
}
2016-04-15
? ? ? ?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!!");
? ? ? ?}
2016-04-15
?
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;//設(shè)置結(jié)果輸入框的值?
? ?}