求大神給我指點(diǎn)迷津
截圖是我的,這是別人的??戳?快倆小時(shí)也沒看出來哪錯(cuò)了。
<!DOCTYPE html>
<html>
?<head>
? <title> 事件</title> ?
? <meta http-equiv="content-type" content="text/html;charset=utf-8" />?
? <script type="text/javascript">
? ?function count(){
? ? ? ?var d = "";
? ? var a=document.getElementById("txt1").value; ??
? ? //獲取第一個(gè)輸入框的值
? ? var b=document.getElementById("txt2").value;
//獲取第二個(gè)輸入框的值
? ? var c=document.getElementById("select").value;
//獲取選擇框的值
? ? switch(c)
{
? ? ?case "+":
? ? ?//d = a + b;
? ? ?d = parseInt(a)+parseInt(b);
? ? ?break;
? ? ?case "-":
? ? ?d = a-b;
? ? ?break;
? ? ?case "*":
? ? ?d = a*b;
? ? ?break;
? ? ?default:
? ? ?d = a/b;
? ? ?}
? ? document.getElementById("fruit").value ? = d;
? ??
? ?}
? </script>?
?</head>?
?<body>
? ?計(jì)數(shù)器:<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-06-06
?獲取值的時(shí)候先全部轉(zhuǎn)為整型,后面運(yùn)算就肯定不會(huì)錯(cuò)了...
var a=parseInt(document.getElementById("txt1").value); ??
var b=parseInt(document.getElementById("txt2").value);
2016-06-06
我找到問題了。謝謝你們回答!
1
?var nan1 = document.getElementById("text1").value;
? ? ? ?//獲取第一個(gè)輸入框的值
? ? var nan2 = document.getElementById("text2").value;
? ? ? ?//獲取第二個(gè)輸入框的值
應(yīng)該是Id應(yīng)該是txt1
2
document.getElementById("fruit")= zhi;
? ? ?//設(shè)置結(jié)果輸入框的值?
少了個(gè)value=zhi;