課程
/前端開發(fā)
/JavaScript
/JavaScript進階篇
搞了半天。
2015-08-18
源自:JavaScript進階篇 6-11
正在回答
<!DOCTYPE html><html>?<head>? <title> 事件</title> ?? <script type="text/javascript">?? function count(){????? ???? //獲取第一個輸入框的值??? var txt1Val = document.getElementById("txt1").value;? //獲取第二個輸入框的值??? var txt2Val = document.getElementById("txt2").value;? //獲取選擇框的值??? var opera =? document.getElementById("select");??? var index =? opera.selectedIndex;??? var operaVal = opera.options[index].value;??? var result;? //獲取通過下拉框來選擇的值來改變加減乘除的運算法則? if(operaVal == "+"){ ??? result = (txt1Val-0) + (txt2Val-0);? }else if(operaVal == "-"){ ?? result = txt1Val - txt2Val;? }else if(operaVal == "*"){?? ??? result = txt1Val * txt2Val;? }else{? ??? result = txt1Val / txt2Val;? }?? console.log(txt1Val+"`````````"+txt2Val+"`````````"+result)??? //設(shè)置結(jié)果輸入框的值 ??? 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()"/> <!--通過 = 按鈕來調(diào)用創(chuàng)建的函數(shù),得到結(jié)果--> ?? <input type='text' id='fruit' />? ??</body></html>
舉報
本課程從如何插入JS代碼開始,帶您進入網(wǎng)頁動態(tài)交互世界
1 回答總算折騰出來
1 回答交作業(yè)了,折騰了半天
1 回答誒,求教一下 是哪里出了問題返回不了計算結(jié)果,已經(jīng)折騰半天了……找不出問題在哪
1 回答哪里有問題了,為什么文本文字“騰訊網(wǎng)”沒有顯示出來?
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2015-12-15
<!DOCTYPE html>
<html>
?<head>
? <title> 事件</title> ?
? <script type="text/javascript">
?? function count(){
????? ?
??? //獲取第一個輸入框的值
??? var txt1Val = document.getElementById("txt1").value;
? //獲取第二個輸入框的值
??? var txt2Val = document.getElementById("txt2").value;
? //獲取選擇框的值
??? var opera =? document.getElementById("select");
??? var index =? opera.selectedIndex;
??? var operaVal = opera.options[index].value;
??? var result;
? //獲取通過下拉框來選擇的值來改變加減乘除的運算法則
? if(operaVal == "+"){ ?
?? result = (txt1Val-0) + (txt2Val-0);
? }else if(operaVal == "-"){
?? result = txt1Val - txt2Val;
? }else if(operaVal == "*"){?? ?
?? result = txt1Val * txt2Val;
? }else{? ?
?? result = txt1Val / txt2Val;
? }
?? console.log(txt1Val+"`````````"+txt2Val+"`````````"+result)
??? //設(shè)置結(jié)果輸入框的值
??? 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()"/> <!--通過 = 按鈕來調(diào)用創(chuàng)建的函數(shù),得到結(jié)果-->
?? <input type='text' id='fruit' />? ?
?</body>
</html>