課程
/前端開發(fā)
/JavaScript
/JavaScript進階篇
怎么獲取下拉選擇框的值?
2016-05-03
源自:JavaScript進階篇 6-11
正在回答
document.getElementById("select").value
LalaYoung 提問者
完整代碼:
<!DOCTYPE html>
<html>
?<head>
? <title> 事件</title> ?
? <script type="text/javascript">
? ?function count(){
? ? ? ?
? ? //獲取第一個輸入框的值
? ? var value0=parseInt(document.getElementById("txt1").value);
//獲取第二個輸入框的值
? ? var value1=parseInt(document.getElementById("txt2").value);
//獲取選擇框的值
? ? var symbol=document.getElementById("select").value;
//獲取通過下拉框來選擇的值來改變加減乘除的運算法則
? ? var fruit;
? ? switch(symbol){
? ? ? ? case "+":fruit=value0+value1;
? ? ? ? break;
? ? ? ? case "-":fruit=value0-value1;
? ? ? ? case "*":fruit=value0*value1;
? ? ? ? case "/":fruit=value0/value1;
? ? }
? ? //設置結果輸入框的值?
? ? document.getElementById("fruit").value=fruit;
? ?}
? </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ù),得到結果-->?
? ?<input type='text' id='fruit' /> ??
?</body>
</html>
document.getElementById("下拉框的id").value() = "下拉框的值";
<html> <head> ????<meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"> ????<title>?卸載事件?</title> ????<script?type="text/javascript"> ???????function?select(){ ????????????var?sel?=?document.getElementById("select").value; ????????????alert('你選擇的值是:'+sel+'你選擇的內(nèi)容是:'+text); ????????} ????</script> </head> <body> <select?id="select"?onchange="select()"> ????<option?value="1">語文</option> ????<option?value="2">數(shù)學</option> ????<option?value="3">幾何</option> </select> </body> </html>
舉報
本課程從如何插入JS代碼開始,帶您進入網(wǎng)頁動態(tài)交互世界
3 回答我想問一下 就這段代碼 前面獲取選擇框內(nèi)的值 需不需要parseInt
5 回答JS獲取輸入框的值
1 回答獲取下拉框的值document.getElementById().value和document.getElementById().options[document.getElementById().selectedIndex].value有什么區(qū)別嗎?
6 回答fruit文本框獲取不到值
1 回答獲取數(shù)值?
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學習伙伴
掃描二維碼關注慕課網(wǎng)微信公眾號
2016-05-03
document.getElementById("select").value
2016-05-07
完整代碼:
<!DOCTYPE html>
<html>
?<head>
? <title> 事件</title> ?
? <script type="text/javascript">
? ?function count(){
? ? ? ?
? ? //獲取第一個輸入框的值
? ? var value0=parseInt(document.getElementById("txt1").value);
//獲取第二個輸入框的值
? ? var value1=parseInt(document.getElementById("txt2").value);
//獲取選擇框的值
? ? var symbol=document.getElementById("select").value;
//獲取通過下拉框來選擇的值來改變加減乘除的運算法則
? ? var fruit;
? ? switch(symbol){
? ? ? ? case "+":fruit=value0+value1;
? ? ? ? break;
? ? ? ? case "-":fruit=value0-value1;
? ? ? ? break;
? ? ? ? case "*":fruit=value0*value1;
? ? ? ? break;
? ? ? ? case "/":fruit=value0/value1;
? ? }
? ? //設置結果輸入框的值?
? ? document.getElementById("fruit").value=fruit;
? ?}
? </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ù),得到結果-->?
? ?<input type='text' id='fruit' /> ??
?</body>
</html>
2016-05-03
document.getElementById("下拉框的id").value() = "下拉框的值";
2016-05-03