按等于號(hào)沒反應(yīng)!
<!DOCTYPE html>
<html>
?<head>
? <title> 事件</title> ?
? <script type="text/javascript">
?? function count(){
???? var? a= parseInt(document.getElementById("txt1").value);
???? var? b= parseInt(document.getElementById("txt2").value);
???? var? c= document.getElementById("select").value;
???? //獲取選擇框的值
???? var d;
???? switch(c)
???? {
???????? case "+":d=(a+b);break;
???????? case "-":d=(a-b);break;
???????? case "*":d=(a*b);break;
???????? case "/":d=(a/b);break;
???????? }
??? ?
??? //獲取第一個(gè)輸入框的值
?? ?//獲取第二個(gè)輸入框的值
?? ?//獲取選擇框的值
?? ?//獲取通過下拉框來選擇的值來改變加減乘除的運(yùn)算法則
??? //設(shè)置結(jié)果輸入框的值
?? 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=' = ' onclick="count()"
2016-03-29
<!DOCTYPE html>
<html>
?<head>
? <title> 事件</title> ?
? <script type="text/javascript">
? ?function count(){
? ? ?var ?a= parseInt(document.getElementById("txt1").value);
? ? ?var ?b= parseInt(document.getElementById("txt2").value);
? ? ?var ?c= document.getElementById("select").value;
? ? ?var ?d;
? ? ?switch(c)
? ? ?{
? ? ? ? ?case "+":d=(a+b);break;
? ? ? ? ?case "-":d=(a-b);break;
? ? ? ? ?case "*":d=(a*b);break;
? ? ? ? ?case "/":d=(a/b);break;
? ? ? ? ?}
? ?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=' = ' onclick="count()"/>
? ?<input type='text' id='fruit'/>
? </body>
? </html>
2016-03-29
??? var? a= parseInt(document.getElementById("txt1").value);
???? var? b= parseInt(document.getElementById("txt2").value);
???? var? c= document.getElementById("select").value;
這里面的小括號(hào)在英文狀態(tài)下書寫。
2016-03-29
你用這個(gè)全局函數(shù)parseFloat試試看
parseFloat(a)+parseFloat(b);
2016-03-29
: 使用parseInt()函數(shù)可解析一個(gè)字符串,并返回一個(gè)整數(shù)。
switch前先將a b c轉(zhuǎn)為int類型
a=parseInt(a);