Ver Mensaje Individual
  #7 (permalink)  
Antiguo 29/10/2012, 13:30
Avatar de emprear
emprear
Colaborador
 
Fecha de Ingreso: junio-2007
Ubicación: me mudé
Mensajes: 8.388
Antigüedad: 16 años, 11 meses
Puntos: 1567
Respuesta: permitir maximo checkbox por grupos

En tanto i crezca siempre será el último. Y el limite debería ser > que no == que
De todas maneras hacer esto es más flexible

Código HTML:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2.    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <title>Contar checkboxes marcados</title>
  5. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  6. <script type="text/javascript">
  7. //<![CDATA[
  8. function limitar(grupo,limite){
  9. var grupo=document.getElementsByName(grupo);
  10. var limite=limite
  11. for (var i=0; i < grupo.length; i++){
  12.  grupo[i].onchange=function(){
  13.  var tildados=0
  14.  for (var i=0; i < grupo.length; i++)
  15.   tildados+=(grupo[i].checked)? 1 : 0
  16.  if (tildados > limite){
  17.   alert("solo se permiten "+limite+" selecciones")
  18.   this.checked=false
  19.   }
  20.  }
  21. }
  22. }
  23.  
  24. //]]>
  25. </head>
  26. <form action="#">
  27. <p>
  28. <input type="checkbox" name="PREGUNTA9[]" onchange="limitar(this.name,3);" /> a<br />
  29. <input type="checkbox" name="PREGUNTA9[]" onchange="limitar(this.name,3);" /> b<br />
  30. <input type="checkbox" name="PREGUNTA9[]" onchange="limitar(this.name,3);" /> c<br />
  31. <input type="checkbox" name="PREGUNTA9[]" onchange="limitar(this.name,3);" /> d<br />
  32. <input type="checkbox" name="PREGUNTA9[]" onchange="limitar(this.name,3);" /> e<br />
  33. </p>
  34.  
  35. <p>
  36. <input type="checkbox" name="PREGUNTA10[]" onchange="limitar(this.name,2);" /> f<br />
  37. <input type="checkbox" name="PREGUNTA10[]" onchange="limitar(this.name,2);" /> g<br />
  38. <input type="checkbox" name="PREGUNTA10[]" onchange="limitar(this.name,2);" /> h<br />
  39. <input type="checkbox" name="PREGUNTA10[]" onchange="limitar(this.name,2);" /> i<br />
  40. <input type="checkbox" name="PREGUNTA10[]" onchange="limitar(this.name,2);" /> j<br />
  41. </p>
  42.  
  43. </form>
  44. </body>
  45. </html>

Podés cambiar el límite para cada grupo a tu gusto cambiando el parámetro

onchange="limitar(this.name,2);"
onchange="limitar(this.name,4);"
onchange="limitar(this.name,3);"
etc.

Saludos
__________________
La voz de las antenas va, sustituyendo a Dios.
Cuando finalice la mutación, nueva edad media habrá
S.R.