Código HTML:
Ver original<meta name="GENERATOR" content="Microsoft FrontPage 6.0"> <meta name="ProgId" content="FrontPage.Editor.Document"> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <form name="editar" action='' method='post'> <!--no puedes usar onchange en los text, usa onkeyup y onkeydown-->
<input type="text" name="p1" value="" size="4" onkeyup="promedio();" onkeydown='promedio();' /> <input type="text" name="p2" value="" size="4" onkeyup="promedio();" onkeydown='promedio();' /> <input type="text" name="p3" value="" size="4" onkeyup="promedio();" onkeydown='promedio();' /> <input type="text" name="p4" value="" size="4" onkeyup="promedio();" onkeydown='promedio();' /> <input type="text" name="p5" value="" size="4" onkeyup="promedio();" onkeydown='promedio();' /> <input type="text" name="p6" value="" size="4" onkeyup="promedio();" onkeydown='promedio();' /> <input type="text" name="p7" value="" size="4" onkeyup="promedio();" onkeydown='promedio();' /> <input type="text" name="pg" value="" size="4" />
Código Javascript
:
Ver original<script type='text/javascript'>
//puedes usar arrays
function promedio(){
var p = new Array();
p[0] = document.editar.p1.value;
p[1] = document.editar.p2.value;
p[2] = document.editar.p3.value;
p[3] = document.editar.p4.value;
p[4] = document.editar.p5.value;
p[5] = document.editar.p6.value;
p[6] = document.editar.p7.value;
for (i=0; i<=p.length; i++){
p[i] = (!isNaN(p[i]) && p[i] != '')? Number(p[i]): 0;
}
//a partir de aquí tienes que cambiar p1 por p[0], p2 por p[1],... pn por p[n-1]
if (p1 != ""){
document.editar.pg.value = p1;
}
if (p1 && p2 != ""){
var suma1 = p1 + p2;
var pg = Number(suma1 / 2);
//aquí entiendo que quieres reducirlo a un decimal ¿no?
//entonces pon toFixed(1)
pg = pg.toFixed(3);
pg = pg.substring(0, pg.length - 2);
document.editar.pg.value = pg
}
if (p1 && p2 && p3 != ""){
var suma2 = p1 + p2 + p3;
var pg = Number(suma2 / 3);
pg = pg.toFixed(3);
pg = pg.substring(0, pg.length - 2);
document.editar.pg.value = pg
}
if (p1 && p2 && p3 && p4 != ""){
var suma3 = p1 + p2 + p3 + p4;
var pg = Number(suma3 / 4);
pg = pg.toFixed(3);
pg = pg.substring(0, pg.length - 2);
document.editar.pg.value = pg
}
if (p1 && p2 && p3 && p4 && p5 != ""){
var suma4 = p1 + p2 + p3 + p4 + p5;
var pg = Number(suma4 / 5);
pg = pg.toFixed(3);
pg = pg.substring(0, pg.length - 2);
document.editar.pg.value = pg
}
if (p1 && p2 && p3 && p4 && p5 && p6 != ""){
var suma5 = p1 + p2 + p3 + p4 + p5 + p6;
var pg = Number(suma5 / 6);
pg = pg.toFixed(3);
pg = pg.substring(0, pg.length - 2);
document.editar.pg.value = pg
}
if (p1 && p2 && p3 && p4 && p5 && p6 && p7 != ""){
var suma6 = p1 + p2 + p3 + p4 + p5 + p6 + p7;
var pg = Number(suma6 / 7);
pg = pg.toFixed(3);
pg = pg.substring(0, pg.length - 2);
document.editar.pg.value = pg
}
}
</script>
Saludos (: