幫忙看看這個代碼,哪里出錯了
<script type="text/javascript">
? ?function count(){
? ? var txt1=parseInt(document.getElementByld("txt1").value);
? ? var txt2=parseInt(document.getElementByld("txt2").value);
? ? var select=document.getElementByld("select").value;
? ? var result?
? ? //獲取第一個輸入框的值
//獲取第二個輸入框的值
//獲取選擇框的值
//獲取通過下拉框來選擇的值來改變加減乘除的運算法則
? ? //設置結果輸入框的值?
? ? switch (select)
? ? {
? ? ? ? case"+":
? ? ? ? ? ? result=txt1 + txt2;
? ? ? ? ? ? break;
? ? ? ? ?case"-":
? ? ? ? ? ? result=txt1 - txt2;
? ? ? ? ? ? break;
? ? ? ? ?case"*":
? ? ? ? ? ? result=txt1 * txt2;
? ? ? ? ? ? break;
? ? ? ? ?case"/":
? ? ? ? ? ? result=txt1 / txt2;
? ? ? ? ? ? break;
? ? }
? ? document.getElementByld("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' /> ??
?</body>
2016-08-25
?document.getElementByld寫錯了。 最后面的ById 不是Byid 。I是大寫的。
下次細心點。
2016-08-26
粗心。getElementById中的I要大寫
2016-08-25
樓上正解
2016-08-25
有兩個地方的錯誤:
getElementById()拼寫錯誤: ? 這里的 Id 是大寫的 i ,而你寫成了 L 的小寫字母 l 了
var result結尾有一個分號 ; 你沒寫,不過這個錯誤好像沒關系
2016-08-25
還有樓上回的也是對的 但是 I ?i 的 大寫 ?不是 l?
2016-08-25
?var txt1=parseInt(document.getElementByld("txt1").value); ? ? 定義的var txt1 ?變量名換一個跟后面的txt1重復
可以對照--同學代碼--看看