為什么沒有顯示結(jié)果那 還有parseint函數(shù)是什么東西?為什么要用它呢?非常感謝!
<!DOCTYPE html>
<html>
?<head>
? <title> 事件</title>?
? <script type="text/javascript">
? function count(){
?????? var d = "";
??? var a=parseInt(document.getElementById("txt1")).value;??
??? //獲取第一個輸入框的值
??? var b=parseInt(document.getElementById("txt2")).value;
??? //獲取第二個輸入框的值
??? var c=document.getElementById("select").value;
?//獲取選擇框的值
??? switch(c)
?{
???? case "+":
???? d = a+b;
???? break;
???? case "-":
???? d = a-b;
???? break;
???? case "*":
???? d = a*b;
???? break;
???? default:
???? d = a/b;
???? }
??? document.getElementById("fruit").value=d;
??? }
??
? </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=' = ' /> <!--通過 = 按鈕來調(diào)用創(chuàng)建的函數(shù),得到結(jié)果-->
?? <input type='text' id='fruit' />??
2016-11-28
document.getElementById().value獲取到的是一個字符串類型的數(shù)據(jù),parseInt方法將這個字符串轉(zhuǎn)換成整型,才可以進行加減乘除運算;你這個地方parseInt方法轉(zhuǎn)換的應(yīng)該是document.getElementById().value,而不是value放到parseInt后面;
count()方法沒有通過事件來調(diào)用,比如<input type='button' value='=' onclick='count()'>
2016-11-28
等號沒有引入方法 ? ?應(yīng)該 ?onclick=“count()” ? ?var d='' ? 單引號