Comunidad de diseño web y desarrollo en internet online

ayuda con funcion checked multiple

Citar            
MensajeEscrito el 06 Ene 2011 02:17 am
Hola. veran tengo este sencillo script que activa el check box si ingreso un dato en el campo de texto, ya que muchas veces lo dejaban vacio... y se supone que si hay un valor en el campo de texto el producto existe. asi como esta funciona muy bien con 1 solo producto.

Código :

<html> 
<head> 
<title>Untitled</title> 
</head> 
<script> 
function activar(){
    alert('entre a funcion');
    var inputTexto = document.miFormulario.precio;
    var checkCaja = document.miFormulario.existe;
    checkCaja.checked=false;
    if(inputTexto.value == ''){ checkCaja.checked=false;
    }else{ checkCaja.checked=true;
  }
}
</script> 
<body> 
<form name="miFormulario"> 
<input type="checkbox" name="existe"> 
<input type="text" name="precio" value="" onchange="activar()">
<br/>  
</form> 
</body> 
</html>


El problema es que son un array de productos que jalo de una base de datos y el nombre final de cada check queda con la id

Código :

<form name="miFormulario"> 
<input type="checkbox" name="existe1"> 
<input type="text" name="precio1" value=""onchange="activar()">
<br/> 
<input type="checkbox" name="existe2"> 
<input type="text" name="precio2" value=""onchange="activar()">
<br/> 
<input type="checkbox" name="existe3"> 
<input type="text" name="precio3" value=""onchange="activar()">
</form>

como lo implemento este script con la id en dicho array [/code]

Por atkdesign

31 de clabLevel



 

diseño web

chrome
Citar            
MensajeEscrito el 06 Ene 2011 04:01 pm
Prueba lo siguiente:

Código HTML :

<html>
<head>
<title>Esto es el título</title>
<script type="text/javascript">
function activar(id) {
   var precio = document.getElementById(id);
   var existe = document.getElementById("chk"+id);
   existe.checked = precio.value != '';
}
</script>
</head>
<body>
<input type="checkbox" name="existe1" id="chk1"> 
<input type="text" name="precio1" value="" id="1" onchange="activar(this.id)">
<br/> 
<input type="checkbox" name="existe2" id="chk2"> 
<input type="text" name="precio2" value="" id="2" onchange="activar(this.id)">
<br/> 
<input type="checkbox" name="existe3" id="chk3"> 
<input type="text" name="precio3" value="" id="3" onchange="activar(this.id)">
</form></body>
</html>

La idea es que asignes los atributos "id" de cada input que se relacionan entre sí de la misma forma que lo has hecho con "name".

Por DriverOp

Claber

2510 de clabLevel



 

opera
Citar            
MensajeEscrito el 11 Ene 2011 01:46 am
gracias ha ido de maravilla

Por atkdesign

31 de clabLevel



 

diseño web

chrome

 

Cristalab BabyBlue v4 + V4 © 2011 Cristalab
Powered by ClabEngines v4, HTML5, love and ponies.