Ver Mensaje Individual
  #1 (permalink)  
Antiguo 06/04/2012, 00:55
Avatar de Threepwood
Threepwood
 
Fecha de Ingreso: febrero-2002
Ubicación: Argentina
Mensajes: 906
Antigüedad: 22 años, 2 meses
Puntos: 4
Contador de checkboxes seleccionados [Solo suma y no resta]

Hola!

Tengo este script cuya función (una de dos) es contar la cantidad de checkboxes seleccionados y no dejar seleccionar más cuando se supera una cantidad establecida. La cantidad de checkboxes se muestra al usuario en un input:

Código HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento sin título</title>
<script type="text/javascript">
var total=10000;

var maxi = 2;

function sumar(valor) {
total += valor; 
document.equipo.total.value=total;
cbs=document.equipo.getElementsByTagName("input")
for (var i = 0; i < cbs.length; i++ ) {
if(cbs[i].type=="checkbox"&&cbs[i].title>total){
cbs[i].disabled=true;
		}
if(cbs[i].type=="checkbox"&&cbs[i].title<total||cbs[i].checked==true){
cbs[i].disabled=false;
		}		
	}
}

function restar(box,valor) {
var cuenta=0;
total-=valor; 
cbs=document.equipo.getElementsByTagName("input")
for (var i = 0; i < cbs.length; i++ ) {
if(cbs[i].type=="checkbox"&&cbs[i].checked==true){
cuenta++
 
if (cuenta > maxi) {    
alert("El equipo no puede tener más de " + maxi + " jugadores");
  cbs[i].disabled=true;
  box.disabled = true
  box.checked = false;
  
  --cuenta;

  total+=valor;

 }

 document.equipo.contador.value = cuenta;
 
  }

if(cbs[i].type=="checkbox"&&cbs[i].title>total){
cbs[i].disabled=true;
		}
if(cbs[i].type=="checkbox"&&cbs[i].title<total||cbs[i].checked==true){
cbs[i].disabled=false;
		}
	}
if (total < 0) {
  alert("No hay más presupuesto");
 }
document.equipo.total.value=total;
}
</script>
</head>
*
<body>
<form name="equipo">
<input name="jugadores[]" id="jugador-7" value="Leo Fernández" title="3600" type="checkbox" onclick="if (this.checked) restar(this,Number(this.title)); else sumar(Number(this.title))" />
<input name="jugadores[]" id="jugador-8" value="José López"  title="5800" type="checkbox" onclick="if (this.checked) restar(this,Number(this.title)); else sumar(Number(this.title))" />
<input name="jugadores[]" id="jugador-9" value="Juan Fulano"  title="100" type="checkbox" onclick="if (this.checked) restar(this,Number(this.title)); else sumar(Number(this.title))" />
<input name="jugadores[]" id="jugador-6" value="Zutano"  title="400" type="checkbox" onclick="if (this.checked) restar(this,Number(this.title)); else sumar(Number(this.title))" />
<input name="total" type="text" value="10000">
<input name="contador" type="text" value="0">
</form>
</body>
</html> 
Ahora bien, todo el script funciona bien salvo que al momento de deseleccionar un checkbox el input "contador" no modifica su valor. El único problema es esto, que contador no resta la cifra cuando se deselecciona el checkbox...

¿Alguna idea para solucionarlo?
__________________
Equívocos sin importancia