Foros del Web » Programando para Internet » Javascript »

Problemas con getElementById

Estas en el tema de Problemas con getElementById en el foro de Javascript en Foros del Web. Buenas Dias Tengo un serio problema con la funcion getElementById() que es el siguiente: De una consulta saco varios articulos que corresponden a una categoria ...
  #1 (permalink)  
Antiguo 21/09/2005, 02:58
Avatar de JuanKa  
Fecha de Ingreso: septiembre-2004
Mensajes: 468
Antigüedad: 19 años, 7 meses
Puntos: 1
Problemas con getElementById

Buenas Dias
Tengo un serio problema con la funcion getElementById() que es el siguiente:

De una consulta saco varios articulos que corresponden a una categoria supongamos Familia HD tiene 23 articulos. Ahora cuando coloco la cantidad por ejemplo en el 5 articulo me envia como parametro el codigo del primer articulo con el valor siempre de 1, asi le ponga yo cualquier cantidad.

Leyendo por alli vi que solo la funcion getElementById() es solo para un elemento.

Es cierto esto.....

Le envio el script.

<form action='Ventas/cesta.php?action=add_item&id=$art[cod_art]' method=post name=frmCantidad class=form id=frmCantidad>
<input name='txt_cantidad' type='text' class='caja' style='text-align:center' onKeypress='Validar_Numeros()' value='1' size='4' maxlength='2' >
<input type='button' name='btn_enviar' value='Enviar' class='boton' onClick='cantidad_comprar()'>
</form>
//*************************************************


function cantidad_comprar(){
var tit = document.getElementById("txt_cantidad").value;
if (parseInt(tit) > 10){
alert ('\nPara realizar compras superiores a 11 Unidades por artículo,\n\n\t Por favor contactenos con nosotros.\n\n\t\t Gracias');
return false;
} else {
document.forms[1].submit();
return true;
}

}

Como podria hacer para que en cualquier text de cualquier articulo me lea el valor que estoy enviando('txt_cantidad') y este dato me lo envie como parametros.

Probe con

var tit=document.frmCantidad.txt_cantidad.value;

pero no recogo su valor.

Gracias y saludos
  #2 (permalink)  
Antiguo 21/09/2005, 09:25
Avatar de Cap.Buscapina  
Fecha de Ingreso: octubre-2004
Ubicación: Argentina
Mensajes: 836
Antigüedad: 19 años, 6 meses
Puntos: 4
esto debería funcionar:

function cantidad_comprar(){
var tit = document.forms['frmCantidad']['txt_cantidad'].value;
if (parseInt(tit) > 10){
alert ('\nPara realizar compras superiores a 11 Unidades por artículo,\n\n\t Por favor contactenos con nosotros.\n\n\t\t Gracias');
return false;
} else {
document.forms['frmCantidad'].submit();
return true;
}

}


Ademas , no se si tiene que ver o no , pero los atributos de las etiquetas deben tener comillas dobles.
__________________
by Capitán Buscapina
.
  #3 (permalink)  
Antiguo 21/09/2005, 09:26
Avatar de JuanKa  
Fecha de Ingreso: septiembre-2004
Mensajes: 468
Antigüedad: 19 años, 7 meses
Puntos: 1
Haber si alguien me puede ayudar con este problema.

Gracias
  #4 (permalink)  
Antiguo 21/09/2005, 09:39
Avatar de Cap.Buscapina  
Fecha de Ingreso: octubre-2004
Ubicación: Argentina
Mensajes: 836
Antigüedad: 19 años, 6 meses
Puntos: 4
ha, y me olvidaba de algo:

getElementById() hace referencia al ID de un objeto, para optener el valor a traves de getElementById() tendrías que adicionarle al input:

id="txt_cantidad"


y si no obtenrlo como el ejemplo que te dejé mas arriba
__________________
by Capitán Buscapina
.
  #5 (permalink)  
Antiguo 21/09/2005, 09:55
Avatar de JuanKa  
Fecha de Ingreso: septiembre-2004
Mensajes: 468
Antigüedad: 19 años, 7 meses
Puntos: 1
Te comento que el error me persiste he colocado el id="txt_Cantidad" en el input

con la funcion que tu me digistes:

************************************************** ****
function cantidad_comprar(){
var tit = document.forms['frmCantidad']['txt_cantidad'].value;
if (parseInt(tit) > 10){
alert ('\nPara realizar compras superiores a 11 Unidades por artículo,\n\n\t Por favor contactenos con nosotros.\n\n\t\t Gracias');
return false;
} else {
document.forms['frmCantidad'].submit();
return true;
}

}
************************************************

No recogo el valor de tit, pero con la funcion que postee:

************************************************
function cantidad_comprar(){
var tit = document.getElementById("txt_cantidad").value;
alert(tit);
if (parseInt(tit) > 10){
alert ('\nPara realizar compras superiores a 11 Unidades por artículo,\n\n\t Por favor contactenos con nosotros.\n\n\t\t Gracias');
return false;
} else {
document.forms[1].submit();
return true;
}

}
**********************************************

SI RECOGO EL VALOR, PERO CUANDO LOS ARTICULOS SON VARIOS SIEMPRE RECOJO EL VALOR DE 1, Y CUANDO EN LA RELACION DE ARTICULOS ES 1 COLOCO CUALQUIER COLOR Y SI ME LO ACEPTA.

REPITO MI PROBLEMA ES CUANDO LA CATEGORIA TIENE VARIOS ARTICULOS SIEMPRE RECOJO EL VALOR DE 1.


Haber si me pueden ayudar.

GRACIAS
  #6 (permalink)  
Antiguo 21/09/2005, 14:54
Avatar de JuanKa  
Fecha de Ingreso: septiembre-2004
Mensajes: 468
Antigüedad: 19 años, 7 meses
Puntos: 1
sigo con el mismo problema ... alguien me podra dar una ayuda.
  #7 (permalink)  
Antiguo 21/09/2005, 16:31
Avatar de Cap.Buscapina  
Fecha de Ingreso: octubre-2004
Ubicación: Argentina
Mensajes: 836
Antigüedad: 19 años, 6 meses
Puntos: 4
tal vez ayudaría si pones el codigo completo con formulario incluído.
__________________
by Capitán Buscapina
.
  #8 (permalink)  
Antiguo 22/09/2005, 08:28
Avatar de JuanKa  
Fecha de Ingreso: septiembre-2004
Mensajes: 468
Antigüedad: 19 años, 7 meses
Puntos: 1
Haber te posteo el Codigo y luego te explico como es la estructura.

la pagina es un index que llama a varios archivos php


1.- <? include ('Plantillas_Index/index_cabecera.php'); ?>
2.- <? include ('Plantillas_Index/plan_izquierda.php')?>
3.- <? include ('Plantillas_Index/plan_centro.php')?>
4.- <? include ('Plantillas_Index/plan_derecho.php')?>
5.- <? include ('Plantillas_Index/index_pie_pagina.php'); ?>
------------------------------------------------------------------
.......................................... 1 ..............................................
------------------------------------------------------------------
............|..................................... .......................|.................
............|..................................... .......................|.................
....2.......|...........................3. .............................|........4 ......
............|..................................... .......................|.................
............|..................................... .......................|.................
------------------------------------------------------------------
.......................................... 5 ..............................................
------------------------------------------------------------------

Bueno ese es el bosquejo que tengo lo que pasa que en dos estan las categorias y en 3 los articulos que corresponden a esa categoria y el problema es cuando tengo varios articulos.

Bueno eso es todo lo que lo puedo decir

********************* index.php*************************
<?php
include("Ventas/config_cesta.php");

$cod=$_GET['codi_fami'];
$sub=$_GET['codi_sub'];

$ok=$_GET['ok'];
$arti=$_GET['arti'];
$ban=0;
$consulta_categorias =mysql_query("SELECT * FROM familias order by nom_fam ",$conectar);

$consu=mysql_query("SELECT cod_sub_fam,nom_sub_fam FROM sub_familias WHERE cod_fam='$cod'",$conectar);

$consul = mysql_query("SELECT * FROM articulos WHERE cod_sub_fam='$sub'",$conectar);

$consulta_novedad=mysql_query("SELECT * FROM articulos WHERE nov_art='Si'",$conectar);
$consulta_ofertas=mysql_query("SELECT * FROM articulos WHERE ofe_art='Si'",$conectar);

$consu_cantidad=mysql_query("SELECT count(*) FROM articulos WHERE cod_sub_fam='$sub'",$conectar);
$num=mysql_result($consu_cantidad,0,0);

$_pagi_sql = "SELECT * FROM articulos WHERE cod_sub_fam='$sub'";
$_pagi_cuantos = 6;

$cantidad=mysql_query("SELECT Sum(can_art) as cant FROM cesta WHERE cookieId='".GetCartId()."' ",$conectar);

$var_cant=mysql_result($cantidad,0,"cant");

$total=mysql_query("SELECT Sum(cesta.can_art*articulos.pre_art) as tot FROM cesta,articulos WHERE cesta.cod_art=articulos.cod_art AND cookieId='".GetCartId()."' ",$conectar);

$var_total=mysql_result($total,0,"tot")*1.16;

?>
<script language="JavaScript" src="JavaScript/validar_ventas.js" type="text/javascript"></script>

<link rel="stylesheet" type="text/css" href="Estilos_CSS/esti_ventas.css">

<? include ('Plantillas_Index/index_cabecera.php'); ?>

<table width="770" align="center" cellpadding="0" cellspacing="0" bgcolor="#ECF3FB">
<tr>
<th scope="col"><table width="765" border="0" align="center" cellpadding="2" cellspacing="2">
<tr>
<th valign="top" bgcolor="#ECF3FB" scope="col">
<? include ('Plantillas_Index/plan_izquierda.php')?>
</th>
<th colspan="2" valign="top" bgcolor="#FCEEBD" scope="col">
<? include ('Plantillas_Index/plan_centro.php')?>
</th>
<th width="165" valign="top" bgcolor="#ECF3FB" scope="col">
<? include ("Plantillas_Index/plan_derecha.php"); ?>
</th>
</tr>
</table>
</th>
</tr>
</table>
<? include ('Plantillas_Index/index_pie_pagina.php'); ?>

********************* fin index.php*************************

[COLOR=DarkRed]*********************** plan_izquierda.php ******************

<table width="170" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#F7C459" class="Middle_Border">
<tr>
<th><div align="center" class="Estilo1">Acceso a clientes</div></th>
</tr>
</table>
<table width="168" align="center" class="Middle">
<tr>
<td>
<FORM action=javascript:valida_clientes(); method=post name=frmClientes class="form" id=frmClientes>
<table width="162">
<tr>
<td class="Estilo12">Usuario</td>
<td><input name="txt_usuario" type="text" class="caja_cliente_index" id="txt_usuario"></td>
</tr>
<tr>
<td class="Estilo12">Clave</td>
<td><input name="txt_clave" type="password" class="caja_cliente_index" id="txt_clave"></td>
</tr>
<tr>
<td colspan="2"><div align="center">
<input name="btn_ingresar" type="submit" class="boton" id="btn_ingresar" value="Ingresar" >
<input name="btn_registrarse" type="submit" class="boton" id="btn_registrarse" value="Registrarse" onClick="nuevo()">
</div></td>
</tr>
</table>
</form></td>
</tr>
</table>
<table width="170" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#F7C459" class="Middle_Border">
<tr>
<td><div align="center" class="Estilo1"><strong>Familias</strong></div></td>
</tr>
</table> <?php
$consulta =mysql_query("SELECT * FROM familias ORDER BY nom_fam ",$conectar);
while ($fam=mysql_fetch_array($consulta)) {
$codi_familia=$fam['cod_fam'];
$nomb_familia=$fam['nom_fam'];
?>

<table width="170" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<th height="25" background="Imagenes/Web/fondo_familia.jpg" class="Estilo13" scope="col"><div align="left" >&nbsp;<img src="Imagenes/Web/boton_mas.jpg" width="9" height="9">&nbsp;<?php echo $nomb_familia; ?></div></th></tr>
<tr>
<td height="5">
<?php $consu_sub=mysql_query("SELECT * FROM sub_familias WHERE cod_fam='$codi_familia' "); ?>

<table width="152" border="0" align="right" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF" background="Imagenes/Web/sub_family.jpg" class="bor">
<?php while ($subfam=mysql_fetch_array($consu_sub))
{
$v=$subfam['cod_sub_fam'];
$consul_registros=mysql_query("SELECT * FROM articulos, sub_familias WHERE (sub_familias.cod_sub_fam=$v) AND (articulos.cod_sub_fam=sub_familias.cod_sub_fam) ");
$numero=mysql_num_rows($consul_registros);
if ($numero != 0)
{
?>
<tr>
<th width="152" height="21" valign="middle" scope="col"><div align="left">&nbsp;&nbsp;&nbsp;&nbsp;<?php echo "<a href=\"index.php?codi_fami=$fam[cod_fam]&codi_sub=$subfam[cod_sub_fam]&ok=1&arti=1\" class='esti3'>$subfam[nom_sub_fam]</a>"; ?></div></th>

</tr>
<?php } //fin de num
} // fin de while ?>
</table>
</td>
</tr>
</table>
<?php } //fin de while ?>


Luego le envio les resto en otro mensaje porque no alcanza
  #9 (permalink)  
Antiguo 22/09/2005, 08:31
Avatar de JuanKa  
Fecha de Ingreso: septiembre-2004
Mensajes: 468
Antigüedad: 19 años, 7 meses
Puntos: 1
********************** plan_centro.php **********************
<table width="220" align="center" cellpadding="0" cellspacing="0" bordercolor="#ECE9D8" bgcolor="#FFFFFF" class="Middle_Border">
<tr>
<th><div align="center"><span class="Estilo9">Relaci&oacute;n de :</span> <span class="Estilo5">
<?php if ($arti==1)
{ echo "ARTICULOS";}
else
{ echo "OFERTAS";}
?>
</span></div></th>
</tr>
</table>


<?php if ($arti==1) {
include("Ventas/paginator.inc.php");
echo "<table width='400' border='0' cellspacing='0' cellpadding='3' bgcolor='#E6E6E6'>";
echo "<tr>";
echo "<th scope='col'><div align='left'><span class='Estilo3'>".$num." Artículos Encontrados"."</span></div></th>";
echo "<th scope='col'><div align='right'><span class='Estilo3'>".$_pagi_navegacion."</span></div></th>";
echo "</tr>";
echo "</table>";

while ($art=mysql_fetch_array($_pagi_result))
{
echo "<table width='400' height='20' border='0' cellpadding='0' cellspacing='0' bgcolor='#E6ECFF' class='Middle_Border'>";
echo "<tr>";
echo "<th class='Estilo4' scope='col'>".$art['nom_art']."</th>";
echo "</tr>";
echo "</table>";
echo "<table width='400' border='0' cellpadding='0' cellspacing='0' bgcolor='#FFFFFF' class='Middle'>";
echo "<tr>";
echo "<th height='91' scope='col'><table width='380' height='80' border='0' cellpadding='0' cellspacing='0'>";
echo "<tr>";
echo "<th width='116' rowspan='3' scope='col'><a href=\"Ventas/ventas_puntual.php?cod=".$art['cod_art']."\"><img src=\"Ventas/thumbnail.php?ruta=../Imagenes/Ventas/".$art['ima1']."\" border='0' alt='Mas Información'></th>";
echo "<th width='67' height='26' scope='col'><div align='left' class='Estilo7'>Codigo</div></th>";
echo "<th width='15' height='26' scope='col'><div align='center' class='Estilo4'>:</div></th>";
echo "<th width='182' height='26' scope='col'><div align='left' class='Estilo9'>".$art['cod_art']."</div></th>";
echo "</tr>";
echo "<tr>";
echo "<td height='26'><div align='left' class='Estilo7'>Precio</div></td>";
echo "<td height='26'><div align='center' class='Estilo4'>:</div></td>";
echo "<td height='26'><div align='left' class='Estilo5'>".$art['pre_art']."<span class='Estilo9'> + IVA </span></div></td>";
echo "</tr>";
echo "<tr>";
echo "<td height='28'><div align='left' class='Estilo7'>Comprar</div></td>";
echo "<td height='28'><div align='center' class='Estilo4'>:</div></td>";
echo "<td height='28'><div align='left'>

<form action='Ventas/cesta.php?action=add_item&id=$art[cod_art]' method=post name=frmCantidad class=form id=frmCantidad>
<input name='txt_cantidad' type='text' class='caja' style='text-align:center' id='txt_cantidad' onKeypress='Validar_Numeros()' value='1' size='4' maxlength='2' >
<input type='button' name='btn_enviar' value='Enviar' class='boton' onClick='cantidad_comprar()'>
</form>


</div></td>";
echo "</tr>";
echo "</table>";
echo "</th>";
echo "</tr>";
echo "</table>";
}
?>
<?php }else { ?>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="0">
<?php while ($ofertas=mysql_fetch_array($consulta_ofertas)) { ?>
<?php if ($ban==0){?>
<tr>
<td width="198" height="198" background="Imagenes/Web/marco.jpg"><div align="center">
<table width="185" height="190" border="0" cellpadding="0" cellspacing="0">
<tr>
<th height="40" scope="col"><?php echo "<a href=\"Ventas/ventas_puntual.php?cod=$ofertas[cod_art]\" class='esti2'>$ofertas[nom_art]</a>"; ?></th>
</tr>
<tr>
<td height="80"><div align="center"><?php echo "<a href=\"Ventas/ventas_puntual.php?cod=".$ofertas['cod_art']."\"><img src=\"Ventas/thumbnail.php?ruta=../Imagenes/Ventas/".$ofertas['ima1']."\" border='0' alt='Mas Informacion'>\n"; ?></div></td>
</tr>
<tr>
<td height="25" valign="middle"><div align="center"><span class="Estilo5"><?php echo $ofertas['pre_art']; $ban=1;?> &euro; + </span><span class="Estilo4">IVA</span></div></td>
</tr>
</table>
</div></td>
<?php } else {?>
<td width="198" height="198" background="Imagenes/Web/marco.jpg"><div align="center"><span class="Estilo4"> </span>
<table width="185" height="190" border="0" cellpadding="0" cellspacing="0">
<tr>
<th height="40" scope="col"><?php echo "<a href=\"Ventas/ventas_puntual.php?cod=$ofertas[cod_art]\" class='esti2'>$ofertas[nom_art]</a>"; ?></th>
</tr>
<tr>
<td height="80"><div align="center"><?php echo "<a href=\"Ventas/ventas_puntual.php?cod=".$ofertas['cod_art']."\"><img src=\"Ventas/thumbnail.php?ruta=../Imagenes/Ventas/".$ofertas['ima1']."\" border='0' alt='Mas Informacion'>\n"; ?></div></td>
</tr>
<tr>
<td height="25" valign="middle"><div align="center"><span class="Estilo5"><?php echo $ofertas['pre_art']; $ban=0;?> &euro; + </span><span class="Estilo4">IVA</span></div></td>
</tr>
</table>
<span class="Estilo5"> </span></div></td>
</tr>
<?php } ?>
<?php } ?>
</table>
<?php } ?>


******************** fin plan_centro.php **********************


y en validar ventas tengo: lo que les estoy preguntando

function cantidad_comprar(){
var tit = document.getElementById("txt_cantidad").value;
alert(tit);

if (parseInt(tit) > 10){
alert ('\nPara realizar compras superiores a 11 Unidades por artículo,\n\n\t Por favor contactenos con nosotros.\n\n\t\t Gracias');
return false;
} else {
document.forms[1].submit();
return true;
}

}
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 01:41.