為什么運行不了,點擊按鈕沒反應(yīng)
大神看看,是哪個地方錯了???
修改后:
<!DOCTYPE html>
<html>
?<head>
? <title> 事件</title> ?
? <script type="text/javascript">
? ?function count(){
? ? ? ?
? ? ?var sr1=document.getElementById("txt1").value;//獲取第一個輸入框的值
? ? ?var sr2=document.getElementById("txt2").value;//獲取第二個輸入框的值
? ? ?var xlk=document.getElementById("select").value;//獲取選擇框的值
? ? ?var fh="";
? ? ?switch (xlk){
? ? ? ? ?case "+":
? ? ? ? ? ? ?fh=parseInt(sr1)+parseInt(sr2);
? ? ? ? ?break;
? ? ? ? ?case "-":
? ? ? ? ? ? ?fh=parseInt(sr1)-parseInt(sr2);
? ? ? ? ?break;
? ? ? ? ?case "*":
? ? ? ? ? ? ?fh=parseInt(sr1)*parseInt(sr2);
? ? ? ? ?break;
? ? ? ? ?default;
? ? ? ? ? ? ?fh=parseInt(sr1)/parseInt(sr2);
? ? ?}//獲取通過下拉框來選擇的值來改變加減乘除的運算法則
? ? ?document.write("fruit").value=fh;//設(shè)置結(jié)果輸入框的值?
? ??
? ?}
? </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>
2016-07-15
<!DOCTYPE html>
<html>
?<head>
? <title> 事件</title> ?
? <script type="text/javascript">
?? function count(){
????? ?
???? var sr1=document.getElementById("txt1").value;//獲取第一個輸入框的值
???? var sr2=document.getElementById("txt2").value;//獲取第二個輸入框的值
???? var xlk=document.getElementById("select").value;//獲取選擇框的值
???? var fh="";
???? switch (xlk){
???????? case "+":
???????????? fh=parseInt(sr1)+parseInt(sr2);
???????? break;
???????? case "-":
???????????? fh=parseInt(sr1)-parseInt(sr2);
???????? break;
???????? case "*":
???????????? fh=parseInt(sr1)*parseInt(sr2);
???????? break;
???????? default:
???????????? fh=parseInt(sr1)/parseInt(sr2);
???? }//獲取通過下拉框來選擇的值來改變加減乘除的運算法則
???? document.getElementById("fruit").value=fh;//設(shè)置結(jié)果輸入框的值
?? ?
?? }
? </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>
2016-07-08
先把 ?default后面的;改成: ?然后再把??document.write("fruit").value=fh;//設(shè)置結(jié)果輸入框的值 ? 這句改成?document.getElementById("fruit").value = fh; ? ??
2016-07-06
default后面應(yīng)該是冒號,而且你的fruit也沒有獲取值,有值了修改fruit的value屬性也不能這么改,直接給fruit.value值就可以。
?var fruit=document.getElementById("fruit");
fruit.value=fh;
2016-07-06
通過getElementById();獲取元素時,括號里的id號要加雙引號,你再試試