Foros del Web » Programando para Internet » Javascript »

Sumar echeckbox

Estas en el tema de Sumar echeckbox en el foro de Javascript en Foros del Web. Hola amigos, Estoy haciendo un script que me permita sumar los valores del checkbox, Ejemplo <input type="checkbox" name="checkbox" value="2>" <input type="checkbox" name="checkbox" value="3>" Al seleccionar ...
  #1 (permalink)  
Antiguo 23/04/2010, 09:38
 
Fecha de Ingreso: febrero-2009
Mensajes: 142
Antigüedad: 15 años, 3 meses
Puntos: 0
Sumar echeckbox

Hola amigos,

Estoy haciendo un script que me permita sumar los valores del checkbox,

Ejemplo
<input type="checkbox" name="checkbox" value="2>"
<input type="checkbox" name="checkbox" value="3>"

Al seleccionar los dos checkbox quiero que me de la suma de ellos es decir que me de= 5, y lo unico que me hace es contar cuantas veces estoy seleccionando .
tengo esto, Ojala upuedan ayudarme.

function suma() {
contador =0;

Form = document.forms["cuestionario"]
for(i = 0; i < Form.elements.length; i++)
if(Form.elements[i]. checked)
if(Form.elements[i].value)

contador++
alert(contador);

if(contador==0){}

}
  #2 (permalink)  
Antiguo 23/04/2010, 10:09
Avatar de gjx2  
Fecha de Ingreso: agosto-2008
Ubicación: R.D
Mensajes: 1.153
Antigüedad: 15 años, 8 meses
Puntos: 139
Respuesta: Sumar echeckbox

JAVASCRIPT:

Código Javascript:
Ver original
  1. var suma = 0;
  2.  
  3.  
  4. function Accion(l){
  5.         if(document.getElementById(l).checked){
  6.             fsuma(document.getElementById(l));
  7.         }else{
  8.             fresta(document.getElementById(l));
  9.         }
  10. }
  11.  
  12.         function fsuma(l){
  13.        
  14.                 suma = eval(suma) + eval(l.value);
  15.             document.getElementById("Resultado").innerHTML = suma;
  16.         }
  17.        
  18.        
  19.         function fresta(l){
  20.            
  21.                     suma = eval(suma) - eval(l.value);
  22.             document.getElementById("Resultado").innerHTML = suma;
  23.            
  24.         }


HTML



Código HTML:
Ver original
  1.  
  2.     1<input type="checkbox" name="l" id="chk_1" value="1" onClick="Accion(this.id)">
  3.     2<input type="checkbox" name="l" id="chk_2" value="2" onClick="Accion(this.id)"><br>
  4.     3<input type="checkbox" name="l" id="chk_3" value="3" onClick="Accion(this.id)"><br>
  5.     4<input type="checkbox" name="l" id="chk_4" value="4" onClick="Accion(this.id)"><br>
  6.     5<input type="checkbox" name="l" id="chk_5" value="5" onClick="Accion(this.id)"><br>
  7.     6<input type="checkbox" name="l" id="chk_6" value="6" onClick="Accion(this.id)"><br>
  8.     7<input type="checkbox" name="l" id="chk_7" value="7" onClick="Accion(this.id)"><br>
  9.  
  10. </form>
  11.  
  12. <div id="Resultado"></div>
  #3 (permalink)  
Antiguo 23/04/2010, 11:42
 
Fecha de Ingreso: febrero-2009
Mensajes: 142
Antigüedad: 15 años, 3 meses
Puntos: 0
Respuesta: Sumar echeckbox

Mil gracias gjX2,

Lo provee y si funciona, si suma los check que selecciono, pero tengo un problema, cuando trato de seleccionar mas check que no correspondan a la misma columna, no lo suma, lo que esta haciendo es restas. Tengo estas preguntas

<td>
Pregunta 1
<input type="checkbox" value="5" name="1" id="check_5" onclick="instuctor(this.id)"> Excelente
<input type="checkbox" value="4" name="1" id="check_4" onclick="instuctor(this.id)">Muy Bueno
<input type="checkbox" value="3" name="1" id="check_3" onclick="instuctor(this.id)">Bueno
<input type="checkbox" value="2" name="1" id="check_2" onclick="instuctor(this.id)">Malo
<input type="checkbox" value="1" name="1" id="check_1" onclick="instuctor(this.id)">Muy Malo
</td>
<tr>
<td>
Pregunta 2
<input type="checkbox" value="5" name="1" id="check_5" onclick="instuctor(this.id)"> Excelente
<input type="checkbox" value="4" name="1" id="check_4" onclick="instuctor(this.id)">Muy Bueno
<input type="checkbox" value="3" name="1" id="check_3" onclick="instuctor(this.id)">Bueno
<input type="checkbox" value="2" name="1" id="check_2" onclick="instuctor(this.id)">Malo
<input type="checkbox" value="1" name="1" id="check_1" onclick="instuctor(this.id)">Muy Malo
</td>
</tr>


Si seleccion un check de la pregunta 1 lo suma, y si despues selecciono un check de pregunta 2 lo resta.

Espero que me puedas apoyar.
  #4 (permalink)  
Antiguo 23/04/2010, 12:52
Avatar de gjx2  
Fecha de Ingreso: agosto-2008
Ubicación: R.D
Mensajes: 1.153
Antigüedad: 15 años, 8 meses
Puntos: 139
Respuesta: Sumar echeckbox

Claro recuerda que el valor de los ID tiene que ser distinto

Podria ser asi

Código HTML:
Ver original
  1. <td>
  2. Pregunta 1
  3. <input type="checkbox" value="5" name="1" id="p1_5" onclick="instuctor(this.id)"> Excelente
  4. <input type="checkbox" value="4" name="1" id="p1_4" onclick="instuctor(this.id)">Muy Bueno
  5. <input type="checkbox" value="3" name="1" id="p1_3" onclick="instuctor(this.id)">Bueno
  6. <input type="checkbox" value="2" name="1" id="p1_2" onclick="instuctor(this.id)">Malo
  7. <input type="checkbox" value="1" name="1" id="p1_1" onclick="instuctor(this.id)">Muy Malo
  8. </td>
  9. <tr>
  10. <td>
  11. Pregunta 2
  12. <input type="checkbox" value="5" name="1" id="p2_5" onclick="instuctor(this.id)"> Excelente
  13. <input type="checkbox" value="4" name="1" id="p2_4" onclick="instuctor(this.id)">Muy Bueno
  14. <input type="checkbox" value="3" name="1" id="p2_3" onclick="instuctor(this.id)">Bueno
  15. <input type="checkbox" value="2" name="1" id="p2_2" onclick="instuctor(this.id)">Malo
  16. <input type="checkbox" value="1" name="1" id="p2_1" onclick="instuctor(this.id)">Muy Malo
  17. </td>
  18. </tr>

Etiquetas: Ninguno
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 05:11.