Foros del Web » Programando para Internet » Javascript »

Ayuda suma variables

Estas en el tema de Ayuda suma variables en el foro de Javascript en Foros del Web. hola tengo un prblema y esque no me suma las variables, me sale un error diciendo 'se esperaba un objeto', os pongo el fragmento del ...
  #1 (permalink)  
Antiguo 19/05/2006, 10:57
 
Fecha de Ingreso: mayo-2006
Mensajes: 22
Antigüedad: 18 años
Puntos: 0
Ayuda suma variables

hola tengo un prblema y esque no me suma las variables, me sale un error diciendo 'se esperaba un objeto', os pongo el fragmento del codigo.

Funcion:
function comprobar()
{
var mio1,mio2,mio3,mio4,mio5,mio6,mio7,mio8,mio9,mio10 ,mio11,mio12,total1,total2;

mio1=document.form2.mio1.value;
mio2=document.form2.mio2.value;
mio3=document.form2.mio3.value;
mio4=document.form2.mio4.value;
mio5=document.form2.mio5.value;
mio6=document.form2.mio6.value;
mio7=document.form2.mio7.value;
mio8=document.form2.mio8.value;
mio9=document.form2.mio9.value;
mio10=document.form2.mio10.value;
mio11=document.form2.mio11.value;
mio12=document.form2.mio12.value;
total1=mio1+mio2+mio3+mio4+mio5+mio6;
total2=mio7+mio8+mio9+mio10+mio11+mio12;
alert(+total1);
alert(+total2);
}


las variables las cojo del siguiente formulario

<form name="form2">
<center>

&nbsp;&nbsp;&nbsp;&nbsp;P. JUGADOR 1: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; P. JUGADOR 2:
<p> <p>
<h3>1</h3><input type="text"name="mio1"> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; <input type="text" name="mio7">
<p> <p>
<h3>2</h3><input type="text" name="mio2"> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; <input type="text" name="mio8">
<p> <p>
<h3>3</h3><input type="text" name="mio3"> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; <input type="text" name="mio9">
<p> <p>
<h3>4</h3><input type="text" name="mio4"> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; <input type="text" name="mio10">
<p> <p>
<h3>5</h3><input type="text" name="mio5"> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; <input type="text" name="mio11">
<p>
<h3>6</h3><input type="text" name="mio6"> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; <input type="text" name="mio12">
<p>
<input type="button" name="Submit" value="comprobar" OnClick="comprobar()">
</center>
</p>
</form>


sabeis donde esta el error???
  #2 (permalink)  
Antiguo 19/05/2006, 11:19
Avatar de JavierB
Colaborador
 
Fecha de Ingreso: febrero-2002
Ubicación: Madrid
Mensajes: 25.052
Antigüedad: 22 años, 2 meses
Puntos: 772
Hola xipi1986

He probado el código y no me da el error que dices Lo que ocurre es que no te hace bien las sumas porque los value son de tipo string y tienes que pasarlos a numérico:

mio1=parseInt(document.form2.mio1.value);

Si tienes que usar valores decimales, cambia parseInt por ParseFloat

Saludos,
  #3 (permalink)  
Antiguo 19/05/2006, 11:44
 
Fecha de Ingreso: mayo-2006
Mensajes: 22
Antigüedad: 18 años
Puntos: 0
ok gracias por contestar tan rapido pero a mi si k me da el error,te paso todo el codigo:
[B]<HTML>
<HEAD>
<SCRIPT>
var numero1,numero2,numero3,numero4,numero5,numero6,nu mero7,acu=0;
var a,b,c,d,e,f,acu=0;
var numero=new String();

numero[0]="1.bmp";
numero[1]="2.bmp";
numero[2]="3.bmp";
numero[3]="4.bmp";
numero[4]="5.bmp";
numero[5]="6.bmp";

function tirar(){;
{
acu++;
if(acu==4||acu==7||acu==10||acu==13||acu==16||acu= =19||acu==22||acu==25||acu==28||acu==31||acu==34|| acu==37)
{
alert('Ya as tirado 3 veces');
alert('Cambio de jugador');
}
a=Math.random()*5;
a=Math.round(a);

b=Math.random()*5;
b=Math.round(b);

c=Math.random()*5;
c=Math.round(c);

d=Math.random()*5;
d=Math.round(d);

e=Math.random()*5;
e=Math.round(e);

img1.src=numero[a];
img2.src=numero;
img3.src=numero[c];
img4.src=numero[d];
img5.src=numero[e];
}
function comprobar()
{
var mio1,mio2,mio3,mio4,mio5,mio6,mio7,mio8,mio9,mio10 ,mio11,mio12,total1,total2;

mio1=document.form2.mio1.value;
mio2=document.form2.mio2.value;
mio3=document.form2.mio3.value;
mio4=document.form2.mio4.value;
mio5=document.form2.mio5.value;
mio6=document.form2.mio6.value;
mio7=document.form2.mio7.value;
mio8=document.form2.mio8.value;
mio9=document.form2.mio9.value;
mio10=document.form2.mio10.value;
mio11=document.form2.mio11.value;
mio12=document.form2.mio12.value;
total1=mio1+mio2+mio3+mio4+mio5+mio6;
total2=mio7+mio8+mio9+mio10+mio11+mio12;
alert(total1);
alert(total2);

}
}</script>
</HEAD>
<BODY id="body1" style="background:green">
<img src="dados2.gif" height="75" width="100" align="left">
<img src="dados3.jpg" height="75" width="100" align="right">
<center>
<form name="form1">
<p>
<input type="checkbox" id="r1" value="checkbox">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="checkbox" id="r1" value="checkbox">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;
<input type="checkbox" id="r1" value="checkbox">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;
<input type="checkbox" id="r1" value="checkbox">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="checkbox" id="r1" value="checkbox">

</form>
<p>
<img src="0.bmp" height="75" width="60" img name="img1">
<img src="0.bmp" height="75" width="60" img name="img2">
<img src="0.bmp" height="75" width="60" img name="img3">
<img src="0.bmp" height="75" width="60" img name="img4">
<img src="0.bmp" height="75" width="60" img name="img5">

</p>
<p>
<input type="button" name="Submit" value="Tirar" OnClick="tirar()">
</p>
</center>
<form name="form2">
<center>

&nbsp;&nbsp;&nbsp;&nbsp;P. JUGADOR 1: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;
P. JUGADOR 2:
<p>
<p>
<h3>1</h3><input type="text"name="mio1"> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;
<input type="text" name="mio7">
<p>
<p>
<h3>2</h3><input type="text" name="mio2"> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;
<input type="text" name="mio8">
<p>
<p>
<h3>3</h3><input type="text" name="mio3"> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;
<input type="text" name="mio9">
<p>
<p>
<h3>4</h3><input type="text" name="mio4"> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;
<input type="text" name="mio10">
<p>
<p>
<h3>5</h3><input type="text" name="mio5"> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;
<input type="text" name="mio11">
<p>
<h3>6</h3><input type="text" name="mio6"> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;
<input type="text" name="mio12">
<p>
<input type="button" name="Submit" value="comprobar" OnClick="comprobar()">
</center>
</p>
</form>
<img src="dados3.jpg" height="75" width="100" align="left">
<img src="dados2.gif" height="75" width="100" align="right">
</BODY>
</HTML>

y perdon por el desorden en el codigo. xD
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 23:07.