求大神看一下哪里錯(cuò),怎么沒輸出
<!DOCTYPE html>
<html>
?<head>
? <title> 事件</title> ?
? <script type="text/javascript">
? ?function count(){
? ? ? ?var result;
? ? // ? document.write("輸出"+one+" "+two+" "+three//+" "+result)
? ? ? ?var one=document.getElementById("txt1").value;
? ? //獲取第一個(gè)輸入框的值
? ? var two=document.getElementById("txt2").value; //獲取第二個(gè)輸入框的值
//獲取選擇框的值
var three=document.getElementById("select").value;
//獲取通過下拉框來選擇的值來改變加減乘除的運(yùn)算法則
switch(three){
? ?case '+':result=one+two;
? ?break;
? ?case "-":
? ? ? ?result=one-two;
? ? ? ?break;
? case "*":
? ? ? result=one*two;
? ? ? break;
? ? ? case "/":
? ? ? result=one/two;
? ? ? break;
? ? ??
}
? ? //設(shè)置結(jié)果輸入框的值?
? ? document.getElementById("fruit")=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()"/> <!--通過 = 按鈕來調(diào)用創(chuàng)建的函數(shù),得到結(jié)果-->?
? ?<input type='text' id='fruit' /> ??
?</body>
</html>
2017-01-11
你可以看下基本類型,現(xiàn)在的one,two是字符串(string),不是數(shù)字(number),無法正常進(jìn)行符號的基本運(yùn)算,你可以通過parseInt(one)+parseInt(two),來吧字符串one和two轉(zhuǎn)化才數(shù)字,以此類推,下面的都需要此類操作
2017-02-03
document.getElementById("fruit")=result;應(yīng)該寫成document.getElementById("fruit").value=result;
另外就是樓上說的
2017-01-11
?var one,two 需要用parseInt()轉(zhuǎn)成整形后才能運(yùn)算