為啥算出來是除法????
<!DOCTYPE html>
<html>
?<head>
? <title> 事件</title> ?
? <script type="text/javascript">
? ?function count(){
? ? var t1=document.getElementById("txt1").value;
? ? var s1=document.getElementById("select").value;
? ? var t2=document.getElementById("txt2").value;
? ??
? ? switch(s1){
? ? ? ? case "+";
? ? ? ? var jieguo=parseInt(s1)+parseInt(s2)
? ? ? ? document.getElementById("fruit")=var jieguo;
? ? ? ? case "-";
? ? ? ? var jieguo=parseInt(s1)-parseInt(s2)
? ? ? ? document.getElementById("fruit")=var jieguo;
? ? ? ? case "*";
? ? ? ? var jieguo=parseInt(s1)*parseInt(s2)
? ? ? ? document.getElementById("fruit")=var jieguo;
? ? ? ? case "/";
? ? ? ? var jieguo=parseInt(s1)/parseInt(s2)
? ? ? ? document.getElementById("fruit")=var jieguo;
? ? }
? </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-04-21
為了能讓你理解,我就修改下你的代碼,請看注釋,代碼其實(shí)可以更簡潔優(yōu)化,如果能看懂,最好像
上面那位同學(xué)那樣寫
2017-04-19
switch循環(huán)里面錯(cuò)的太多了,有點(diǎn)不忍直視。。我把我的發(fā)了你對比看看
<!DOCTYPE html>
<html>
?<head>
? <title> 事件</title> ?
? <script type="text/javascript">
? ?function count(){
? ? var x = document.getElementById("txt1").value;
? ? //獲取第一個(gè)輸入框的值
? ? var y = document.getElementById("txt2").value;
//獲取第二個(gè)輸入框的值
? ? var sel = document.getElementById("select").value;
//獲取選擇框的值
? ? var result="";
? ? ?//獲取通過下拉框來選擇的值來改變加減乘除的運(yùn)算法則
? ? switch(sel){
? ? ? ? case "+":
? ? ? ? result=parseFloat(x)+parseFloat(y);
? ? ? ? break;
? ? ? ? case "-":
? ? ? ? result=parseFloat(x)-parseFloat(y);
? ? ? ? break;
? ? ? ? case "*":
? ? ? ? result=parseFloat(x)*parseFloat(y);
? ? ? ? break;
? ? ? ? default:
? ? ? ? result=parseFloat(x)/parseFloat(y);
? ? ? ? }
? ? ? ? ?document.getElementById("fruit").value=result;
? ? //設(shè)置結(jié)果輸入框的值?
? ? }
? </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>