<!DOCTYPE?html>
<html>
?<head>
??<title>?事件</title>??
??<script?type="text/javascript">
????function?count(){
????var?a=document.getElementById("txt1").value;//取出id賦值給a
????var?b=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"/":
if(b==0){
alert("除數不能為0。");
}
else{
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='?=?'?/?onclick="count()">?<!--通過?=?按鈕來調用創建的函數,得到結果-->?
???<input?type='text'?id='fruit'?/>???
?</body>
</html>
2015-12-10
文本框輸入的事string類型,要拿出里面數據計算要強轉成整形,用parseInt或者Number都行
2015-12-10
這樣的情況下會把“+”當作連接符。
2015-12-10
需要把a和b轉換成int型 ,如果不強轉他會當做字符串處理