為什么我的加法運算不對
<!DOCTYPE html>
<html>
?<head>
? <title> 事件</title>??
? <script type="text/javascript">
? ?function count()
? ?{
? ? ? ?
? ? ? ?var text1=document.getElementById("txt1").value;
? ? ? ?var text2=document.getElementById("txt2").value;
? ? ? ?var v=document.getElementById("select").value;
? ??
? ? //設(shè)置結(jié)果輸入框的值
? ? ? ?switch(v)
? ? ? ?{
? ? ? ? ? ? case "+":
? ? ? ? ? ? var result = text1 + text2;
? ? ? ? ? ? break;
? ? ? ? ? ? case "-":
? ? ? ? ? ? var result = text1 - text2;
? ? ? ? ? ? break;
? ? ? ? ? ? case "*":
? ? ? ? ? ? var result = text1 * text2;
? ? ? ? ? ? break;
? ? ? ? ? ? case "/":
? ? ? ? ? ? var result = text1 / text2;
? ? ? ? ? ? break;
? ? ? ? ? ??
? ? ? ?}
? ? ? ?document.getElementById("fruit").value=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>
2018-11-09
從標(biāo)簽欄中輸入的字符是字符串類型,需要用parseInt(String)方法轉(zhuǎn)換成int類型才可以進(jìn)行運算
2018-11-29
我覺的你沒有先申明result,var result="";
2018-11-08
你這個屬于字符串相加? 并不是數(shù)字之間的運算 需要用parseInt()去解析 得到返回的數(shù)字才能進(jìn)行運算
2018-11-02
var result = parseInt(text1) +parseInt( text2)
加一個parseInt(string, radix) 函數(shù),解析一個字符串,并返回一個整數(shù)。
給運算加上進(jìn)制。
我猜的。