課程
/前端開發
/JavaScript
/JavaScript進階篇
var txt1=document.getElementById("txt1").value;
得不到值是怎么回事?
2016-08-18
源自:JavaScript進階篇 6-11
正在回答
這樣得到的是字符串,你可以用旁邊提示的parseInt轉換,也可以通過value-0來轉~
看一下代碼你就知道了:
<!DOCTYPE html>
<html>
?<head>
? <title> 事件</title> ?
? <script type="text/javascript">
? ?function count(){
? ? ? ?
? ? //獲取第一個輸入框的值
? ? var firstNum=document.getElementById("txt1").value;
? ? firstNum=parseInt(firstNum);
//獲取第二個輸入框的值
? ? var secondNum=document.getElementById("txt2").value;
? ? secondNum=parseInt(secondNum);
//獲取選擇框的值
? ? var mySelection=document.getElementById("select").value;
//獲取通過下拉框來選擇的值來改變加減乘除的運算法則
? ? var result=0;
? ? switch(mySelection){
? ? ? ? case '+':
? ? ? ? ? ? result=firstNum+secondNum;
? ? ? ? ? ? break;
? ? ? ? case '-':
? ? ? ? ? ? result=firstNum-secondNum;
? ? ? ? case '*':
? ? ? ? ? ? result=firstNum*secondNum;
? ? ? ? case '/':
? ? ? ? ? ? result=firstNum/secondNum;
? ? }
? ? //設置結果輸入框的值?
? ? 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()"/> <!--通過 = 按鈕來調用創建的函數,得到結果-->?
? ?<input type='text' id='fruit' value=""/> ??
?</body>
</html>
舉報
本課程從如何插入JS代碼開始,帶您進入網頁動態交互世界
1 回答關于getElementById().value這個指令所得到的值是不是不能拿來直接運算
1 回答為什么不能用getElementById("xxx").value來得到秒數呢
1 回答為什么不能直接用document.getElementById("txt1")獲取數值啊 還要用一個var txt1 = parseInt( document.getElementById('txt1').value);
2 回答function count(){ var txt1 = parseInt( document.getElementById('txt1').value); 最后加.value是什么意思?
2 回答getElementById("wb").value;返回值是字符串嗎?
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2016-08-18
這樣得到的是字符串,你可以用旁邊提示的parseInt轉換,也可以通過value-0來轉~
2016-08-18
看一下代碼你就知道了:
<!DOCTYPE html>
<html>
?<head>
? <title> 事件</title> ?
? <script type="text/javascript">
? ?function count(){
? ? ? ?
? ? //獲取第一個輸入框的值
? ? var firstNum=document.getElementById("txt1").value;
? ? firstNum=parseInt(firstNum);
//獲取第二個輸入框的值
? ? var secondNum=document.getElementById("txt2").value;
? ? secondNum=parseInt(secondNum);
//獲取選擇框的值
? ? var mySelection=document.getElementById("select").value;
//獲取通過下拉框來選擇的值來改變加減乘除的運算法則
? ? var result=0;
? ? switch(mySelection){
? ? ? ? case '+':
? ? ? ? ? ? result=firstNum+secondNum;
? ? ? ? ? ? break;
? ? ? ? case '-':
? ? ? ? ? ? result=firstNum-secondNum;
? ? ? ? ? ? break;
? ? ? ? case '*':
? ? ? ? ? ? result=firstNum*secondNum;
? ? ? ? ? ? break;
? ? ? ? case '/':
? ? ? ? ? ? result=firstNum/secondNum;
? ? ? ? ? ? break;
? ? }
? ? //設置結果輸入框的值?
? ? 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()"/> <!--通過 = 按鈕來調用創建的函數,得到結果-->?
? ?<input type='text' id='fruit' value=""/> ??
?</body>
</html>