Ver Mensaje Individual
  #2 (permalink)  
Antiguo 15/01/2010, 02:41
quimfv
Colaborador
 
Fecha de Ingreso: marzo-2008
Ubicación: Sabadell
Mensajes: 4.897
Antigüedad: 16 años, 2 meses
Puntos: 574
Respuesta: Sumar valor a medida que se clickean checkbox

Quito el php,
Trabajo con enteros (100,50 --> 10050) para eliminar los problemas de redondeos...

Recuerda formatear precio antes de guardarlo en la bbdd.... 100,50 ---> 100.50

Quim


Código HTML:
Ver original
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <title>Documento sin t&iacute;tulo</title>
  3. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  4. <script language="JavaScript" type="text/JavaScript">
  5. function suma(obj){
  6.   total=parseInt(document.getElementById("precio").value.replace(",",""));
  7.     if(obj.checked){
  8.         total+=parseInt(obj.value);
  9.     }else{
  10.         total-=parseInt(obj.value);
  11.     }
  12.     txttotal=total+"";
  13.     if (txttotal=="0") txttotal="000";
  14.     document.getElementById("precio").value=txttotal.substring(0,txttotal.length-2)+","+txttotal.substring(txttotal.length-2);
  15. }
  16. </head>
  17. <table width="50%" border="1" cellspacing="0" cellpadding="0">
  18.   <tr>
  19.     <td>Cabalgata&nbsp;</td>
  20.     <td>&nbsp;10,50€&nbsp;</td>
  21.     <td><input type="checkbox" name="exc1" id="exc1" value="1050" onChange="suma(this)"/></td>
  22.   </tr>
  23.   <tr>
  24.     <td>Paseo en bote&nbsp;</td>
  25.     <td>&nbsp;20,00€&nbsp;</td>
  26.     <td><input type="checkbox" name="exc2" id="exc2" value="2000" onChange="suma(this)"/></td>
  27.   </tr>
  28.   <tr>
  29.     <td>Ski&nbsp;</td>
  30.     <td>&nbsp;30,30€&nbsp;</td>
  31.     <td><input type="checkbox" name="exc3" id="exc3" value="3030" onChange="suma(this)"/></td>
  32.   </tr>
  33.   <tr>
  34.     <td>TOTAL:&nbsp;</td>
  35.     <td>&nbsp;&nbsp;</td>
  36.     <td><input type="text" name="precio" id="precio" value="0,00" /></td>
  37.   </tr>
  38. </body>
  39. </html>