Yksinkertainen toteutus laskimesta.
<html>
<head>
<style>
.btn{
width:25px;
}
#naytto{
background:#FFFFAA;
}
</style>
</head>
<body>
<form name="laskin" method="post" action="" />
<table style="background:#D2D2D2; border:1px solid black;">
<tr>
<td>
<input type="text" value="" id="naytto" readonly />
</td>
</tr>
<tr>
<td>
<input type="button" class="btn" value="0" onclick="Laskutoimitus(this, 0);" />
<input type="button" class="btn" value="1" onclick="Laskutoimitus(this, 1);" />
<input type="button" class="btn" value="2" onclick="Laskutoimitus(this, 2);" />
<input type="button" class="btn" value="3" onclick="Laskutoimitus(this, 3);" />
<input type="button" class="btn" value="/" onclick="Laskutoimitus(this, '/');" />
</td>
</tr>
<tr>
<td>
<input type="button" class="btn" value="4" onclick="Laskutoimitus(this, 4);" />
<input type="button" class="btn" value="5" onclick="Laskutoimitus(this, 5);" />
<input type="button" class="btn" value="6" onclick="Laskutoimitus(this, 6);" />
<input type="button" class="btn" value="7" onclick="Laskutoimitus(this, 7);" />
<input type="button" class="btn" value="*" onclick="Laskutoimitus(this, '*');" />
</td>
</tr>
<tr>
<td>
<input type="button" class="btn" value="8" onclick="Laskutoimitus(this, 8);" />
<input type="button" class="btn" value="9" onclick="Laskutoimitus(this, 9);" />
<input type="button" class="btn" value="+" onclick="Laskutoimitus(this, '+');" />
<input type="button" class="btn" value="-" onclick="Laskutoimitus(this, '-');" />
<input type="button" class="btn" value="c" onclick="Laskutoimitus(this, 'c');" />
</td>
</tr>
<tr>
<td>
<input type="button" style="width:100%;" value="Yhteensä" onclick="Laskutoimitus(this, 'laske');" />
</td>
</tr>
</table>
</form>
<script>
function Laskutoimitus(form, arvo){
var laskutoimitus = 0;
if(arvo == 'c'){
document.forms.laskin.submit();
}
if(arvo == 'laske'){
var summa = document.getElementById("naytto").value;
var laskutoimitus = eval(summa);
var laskutoimitus = parseFloat(laskutoimitus)
}
if(laskutoimitus != 0){
document.getElementById("naytto").value = laskutoimitus;
}
else{
document.getElementById("naytto").value += arvo;
}
}
</script>
</body>
</html>Kiva, että muistit kommentoida koodin ja kertoa siitä jotakin.
Aihe on jo aika vanha, joten et voi enää vastata siihen.