我用的是if else語句,為什么不能顯示結(jié)果啊,哪里出錯了,跪求各位大神幫幫忙!??!
<!DOCTYPE html>
<html>
?<head>
? <title> 事件</title> ?
? <script type="text/javascript">
?? function count(){
???? var a=document.getElementById("txt1").value; ?
??? //獲取第一個輸入框的值
??? var b=document.getElmentById("txt2").value;
?? ?//獲取第二個輸入框的值
??? var c=document.getElmentById("select").value;
?? ?//獲取選擇框的值
??? var num=document.getElmentById("fruit").value;
?? ?//獲取通過下拉框來選擇的值來改變加減乘除的運算法則
??? if(c=="+"){
??????? num=a+b;
??? }
??? else if(c=="-"){
??????? num=a-b;
??? }
??? else if(c=="*"){
??????? num=a*b;
??? }
??? else if(c=="/"){
??????? num=a/b;
??? }
??? //設(shè)置結(jié)果輸入框的值
??? document.write(num);
?? }
? ?
? </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>
2016-07-28
function count(){
??? //獲取第一個輸入框的值
??????? var fir = document.getElementById("txt1").value;? ?
?? ?//獲取第二個輸入框的值
??????? var sec = document.getElementById("txt2").value;? ?
?? ?//獲取選擇框的值
??????? var oselect = document.getElementById("select").value;
?? ?//獲取通過下拉框來選擇的值來改變加減乘除的運算法則
??????? var num ;
??????? function count(x,y){
??????????? if(oselect=="+"){
??????????????? num =(x-0) + (y-0);
??????????? }
??????????? else if(oselect=="-"){
??????????????? num = (x-0)-(y-0);
??????????? }
??????????? else if(oselect=="*"){
??????????????? num = (x-0)*(y-0);
??????????? }
??????????? else if(oselect=="/"){
??????????????? num = (x-0)/(y-0);
??????????? }
??????? }
??????? count(fir,sec);
??? //設(shè)置結(jié)果輸入框的值
??????? document.getElementById("fruit").value=num;
?? }
//我用if寫的? 你看看
2016-07-27
<!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 num=document.getElmentById("fruit").value;
? ? //獲取通過下拉框來選擇的值來改變加減乘除的運算法則
? ? if(c=="+"){
? ? ? ? document.getElementById("fruit").value=a+b;
? ? }
? ? else if(c=="-"){
? ? ? ? document.getElementById("fruit").value=a-b;
? ? }
? ? else if(c=="*"){
? ? ? ? document.getElementById("fruit").value=a*b;
? ? }
? ? else if(c=="/"){
? ? ? ? document.getElementById("fruit").value=a/b;
? ? }
? ? //設(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()"/> <!--通過 = 按鈕來調(diào)用創(chuàng)建的函數(shù),得到結(jié)果-->?
? ?<input type='text' id='fruit' /> ??
?</body>
</html>
首先你的getElementById拼錯,其次你沒有對獲取的數(shù)字加上parseInt()
2016-07-27
樓上說的有道理,但是如果這樣的話一開始的聲明就沒必要了直接var num;就行了,還有要改的是
var b=document.getElmentById("txt2").value; ?//以下三個getElementById?都少了1個e
??? var c=document.getElmentById("select").value;
??? var num=document.getElmentById("fruit").value;
還有? var a=document.getElementById("txt1").value; ?//得來的值是字符型要轉(zhuǎn)換為整型
另外如果想用直接改結(jié)果框的內(nèi)容的話可以這樣:
? ? var num=document.getElementById("fruit");
? ? ? ?a = parseInt(a); ?//字符轉(zhuǎn)整型
? ? ? ?b = parseInt(b);
? ? if(c=="+"){
? ? ? ? num.value = a+b;
? ? }
? ? else if(c=="-"){
? ? ? ? num.value=a-b;
? ? }
? ? else if(c=="*"){
? ? ? ? num.value=a*b;
? ? }
? ? else if(c=="/"){
? ? ? ? num.value=a/b;
? ? }
2016-07-27
而且你需要將字符串轉(zhuǎn)化為數(shù)字才行的
2016-07-27
同學(xué),document.write(num);這段代碼不是設(shè)置結(jié)果框中的值,而是在屏幕上打印num的值。這樣才對document.getElementById("fruit").value=num
2016-07-27
?i have no way