简体   繁体   中英

Trouble adding digits to my calculator display

 <html>
 <head>
 <title> Buttons</title>
 <style type="text/css">

.intro{background-color:;}
.duction{background-color:blue;}
.function{background-color:grey;}
.equals{background-color:orange;}
</style>





</head>
<body>
<form name="calculator">
<input type="text"  name="display" length="50" width="100">
<div>
<input type= "button" value="7" class="intro" id="7" onclick="one(7)"></button>
<input type= "button" value="8" class="intro" id="8" onclick="one(8)"></button>
<input type= "button" value="9" class="intro" id="9" onclick="one(9)"></button>
<input type= "button" value="+" class="intro" id="+" onclick="one(+)"></button>
<input type= "button" value="-" class="intro" id="-" onclick="one(-)"></button>
<div>
<input type= "button" value="4" class="intro" id="4" onclick="one(4)"></button>
<input type= "button" value="5" class="intro" id="5" onclick="one(5)"></button>
<input type= "button" value="6" class="intro" id="6" onclick="one(6)"></button>
<input type= "button" value="x" class="intro" id="x" onclick="one(*)"></button>
<input type= "button" value="/" class="intro" id="/" onclick="one(/)"></button>
<div>
<input type= "button" value="1" class="intro" id="1" onclick="one(1)"></button>
<input type= "button" value="2" class="intro" id="2" onclick="one(2)"></button>
<input type= "button" value="3" class="intro" id="3" onclick="one(3)"></button>
<input type= "button" value="=" class="intro" ></button>
<div>
<input type= "button" value="0" class="intro" id="0" onclick="one(0)"></button>
<input type= "button" value="." class="intro" id="." onclick="one(.)"></button>
<input type= "button" value="c" class="intro" onclick="clearDigit()"></button>

<script type="text/javascript" src="C:\Users\LS\Desktop\QBJS\button.js">



</script>



</body>
</html>

Javascript

function one(event)
{
clearTimeout(timer);
timer=setTimeout("AddDigit(object)",500);
object=event;
}

//Everytime I type a number into my display window it replaces the previous number instead of concatenating to it like a an actual calculator would. Also trying to figure out why I cant get my function symbols to appear in my display window....I suspect it has something to do with my setTimeout....

  function AddDigit(x)
  {
  object=x;
  if (eval(digit) == 0)
   {digit = object;}
   else
   {digit = digit + object;}

 document.calculator.display.value=object;

 } 

It isn't concatenating, it is not even adding. You cannot pass like that. The reason is that when you issue a callback with a string, it only will call the function. If you want to pass parameters then you need to create a closure and pass them in there.

Try this:

timer=setTimeout(function(){ AddDigit(object); },500);

More than likely what you meant to pass was the event:

timer=setTimeout(function(){ AddDigit(event); },500);

or

object = event;
timer=setTimeout(function(){ AddDigit(object); },500);

Also, in AddDigit, it is possible you wanted this instead:

document.calculator.display.value += object;

You're doing,

document.calculator.display.value=object;

But the concatenated version is the "digit" variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM