Foros del Web » Programando para Internet » Jquery »

[SOLUCIONADO] validar input solo numeros de un tr en jquery

Estas en el tema de validar input solo numeros de un tr en jquery en el foro de Jquery en Foros del Web. hola a todos, le presento la siguiente consulta tengo el siguiente codigo donde quiero validar que en un input solo ingresen valores numericos el cual ...
  #1 (permalink)  
Antiguo 23/10/2014, 14:22
Avatar de derleguan  
Fecha de Ingreso: abril-2012
Mensajes: 26
Antigüedad: 12 años
Puntos: 1
validar input solo numeros de un tr en jquery

hola a todos, le presento la siguiente consulta

tengo el siguiente codigo donde quiero validar que en un input solo ingresen valores numericos el cual funciona.

Código HTML:
Ver original
  1. <!DOCTYPE HTML>
  2. <html><script id="tinyhippos-injected">if (window.top.ripple) { window.top.ripple("bootstrap").inject(window, document); }</script><head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  4.  
  5.  
  6. <title>Cotizacion</title>
  7.  <script src="jquery.js"></script>
  8.  <script src="jqueryui.js"></script>
  9.  <script>
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17. $(document).ready(function(){
  18. $(".unidad").keydown(function(event) {
  19.    if(event.shiftKey)
  20.    {
  21.         event.preventDefault();
  22.    }
  23.  
  24.    if (event.keyCode == 46 || event.keyCode == 8)    {
  25.    }
  26.    else {
  27.         if (event.keyCode < 95) {
  28.          if (event.keyCode < 48 || event.keyCode > 57) {
  29.                 event.preventDefault();
  30.           }
  31.         }
  32.         else {
  33.               if (event.keyCode < 96 || event.keyCode > 105) {
  34.                   event.preventDefault();
  35.               }
  36.         }
  37.       }
  38.    });
  39. });
  40.  
  41.  
  42.  
  43.  
  44.  </script>
  45. </head>
  46.  
  47.  
  48.     <section id="contenedor">
  49.             <aside id="menu"><h1>Cotizacion</h1></aside>
  50.            
  51.             <table id="tabla">
  52.                 <thead><tr><th></th><th>Nombre</th><th>Precio</th><th>Unidad de medida</th><th>Medida</th><th>Cantidad</th><th>Subtotal</th><th></th></tr></thead>
  53.                 <tbody>
  54.                     <tr>
  55.                         <td>1</td>
  56.                         <td><select name="nombre" required="">
  57.                              <option value="0">Elegir una opciones</option>
  58.                              <option value="1">adhesivo</option>
  59.                              <option value="2">tela</option>
  60.                              <option value="3">microperforado</option>
  61.                              <option value="4">a</option>
  62.                              <option value="5">premaskin</option>
  63.                              <option value="6">piocha</option>
  64.                              <option value="7">k</option>
  65.                         </select></td>
  66.                         <td>2500</td>
  67.                         <td>unidad</td>
  68.                         <td>---</td>
  69.                         <td><input class="unidad" onchange="cambio_unidad()" type:number="" min="0" value="1">
  70.                         <td><label>unidad</label></td>
  71.                         <td>2500</td>
  72.                     </tr>
  73.                 </tbody>
  74.             </table>   
  75.        
  76.            
  77.     </section>
  78.    
  79.  
  80. <input type="text" class="unidad">
  81.                
  82.                 </body></html>



pero al hacer los campos dinamicos no me funciona como se ve en el siguiente codigo


Código HTML:
Ver original
  1. <!DOCTYPE HTML>
  2. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  3.  
  4.  
  5. <title>Cotizacion</title>
  6.  <script src="jquery.js"></script>
  7.  <script src="jqueryui.js"></script>
  8.  <script>
  9.   $(function() {
  10.     $( "#tabs" ).tabs();
  11.   });
  12.   $(function(){
  13.     // Clona la fila oculta que tiene los campos base, y la agrega al final de la tabla
  14.     $("#agregar").on('click', function(){
  15.         $("#tabla tbody tr:eq(0)").clone().removeClass('fila-base').appendTo("#tabla tbody");
  16.     });
  17.     // Evento que selecciona la fila y la elimina
  18.     $(document).on("click",".eliminar",function(){
  19.         var parent = $(this).parents().get(0);
  20.         $(parent).remove();
  21.     });
  22. });
  23.  
  24.  
  25.  
  26. //crear la dotos de la tabla al cargar el documento
  27. $(document).ready(function(){
  28.     //validad solo numeros en input
  29.  
  30.  
  31.     var tbody = document.getElementById ("tabla").getElementsByTagName("TBODY")[0];
  32.     var row = document.createElement("TR")
  33.     var td1 = document.createElement("TD")
  34.     td1.appendChild(document.createTextNode("columna 1"))
  35.     var td2 = document.createElement("TD")
  36.     td2.appendChild (document.createTextNode("columna 2"))
  37.     var td3 = document.createElement("TD")
  38.     td3.appendChild (document.createTextNode("columna 3"))
  39.     var td4 = document.createElement("TD")
  40.     td4.appendChild (document.createTextNode("columna 4"))
  41.     var td5 = document.createElement("TD")
  42.     td5.appendChild (document.createTextNode("columna 5"))
  43.     var td6 = document.createElement("TD")
  44.     td6.appendChild (document.createTextNode("columna 6"))
  45.     var td7 = document.createElement("TD")
  46.     td7.appendChild (document.createTextNode("columna 7"))
  47.     row.appendChild(td1).innerHTML="1";
  48.     row.appendChild(td2).innerHTML="<?php
  49.                 require( "variables.php" );
  50.  
  51.                 $h = mysql_connect( $host, $admin_name, $admin_pass ) or die( "ERROR al conectar al servidor." );
  52.                 if( isset( $h ) ){
  53.                     $db = mysql_select_db( $db_name, $h ) or die( "ERROR al abrir base de datos." );
  54.                     if( isset( $db ) ){
  55.                         $sql = " SELECT * from productos ";
  56.                         $res = mysql_query( $sql, $h ) or die( "ERROR al ejecutar instruccion SQL." );
  57.                         echo "<select name=nombre required >  ";
  58.                         echo "<option value=0>Elegir una opciones</option>";
  59.                         $i=1;
  60.                         while( $fila = mysql_fetch_row( $res ) ){
  61.                                 echo "<option value=".$i.">".$fila[1]."</option>";
  62.                             $i++;
  63.                         }
  64.                         echo "</select>";
  65.                     }  
  66.                 }    
  67.                 mysql_close($h);
  68.                 ?>";
  69.     row.appendChild(td3).innerHTML="0";
  70.     row.appendChild(td4).innerHTML="---------";
  71.     row.appendChild(td5).innerHTML="0";
  72.     row.appendChild(td6).innerHTML="0";
  73.     row.appendChild(td7).innerHTML="0";
  74.     tbody.appendChild(row);
  75.  
  76. $(".unidad").keydown(function(event) {
  77.    if(event.shiftKey)
  78.    {
  79.         event.preventDefault();
  80.    }
  81.  
  82.    if (event.keyCode == 46 || event.keyCode == 8)    {
  83.    }
  84.    else {
  85.         if (event.keyCode < 95) {
  86.          if (event.keyCode < 48 || event.keyCode > 57) {
  87.                 event.preventDefault();
  88.           }
  89.         }
  90.         else {
  91.               if (event.keyCode < 96 || event.keyCode > 105) {
  92.                   event.preventDefault();
  93.               }
  94.         }
  95.       }
  96.    });
  97.  
  98.     $("select").change(function(){
  99.         var str = "";
  100.         $( "select option:selected" ).each(function() {
  101.           str += $( this ).text() + " ";
  102.         });
  103.         //alert(str);
  104.         $.post("jquery_ajax_consulta.php",
  105.         {
  106.           nombre:str,
  107.         },
  108.         function(data,status){
  109.  
  110.             var valores= data;
  111.             var datos= valores.split(',');
  112.             document.getElementById("tabla").rows[1].cells[2].innerHTML =datos[0];
  113.             if(parseInt(datos[1])==1){
  114.                 document.getElementById("tabla").rows[1].cells[3].innerHTML ="unidad";
  115.                 document.getElementById("tabla").rows[1].cells[4].innerHTML ="<tr>---</tr>";
  116.                 document.getElementById("tabla").rows[1].cells[5].innerHTML ="<input class=unidad  onchange=cambio_unidad() type:number min=0 value=1><label>unidad</label> ";
  117.                 document.getElementById("tabla").rows[1].cells[6].innerHTML =datos[0];
  118.             }else if (parseInt(datos[1])==2) {
  119.                 document.getElementById("tabla").rows[1].cells[3].innerHTML ="M. lineal";
  120.                 document.getElementById("tabla").rows[1].cells[4].innerHTML ="<label>Largo:</label><input name=largo type:number min=0 value=100><label>cm</label>";
  121.                 document.getElementById("tabla").rows[1].cells[2].innerHTML =datos[0];
  122.                 document.getElementById("tabla").rows[1].cells[6].innerHTML =datos[0];
  123.             }else if (parseInt(datos[1])==3) {
  124.                 document.getElementById("tabla").rows[1].cells[3].innerHTML ="M&sup2";
  125.                 document.getElementById("tabla").rows[1].cells[4].innerHTML ="<label>Ancho:</label><input name=Ancho type:number min=0 value=100><label>cm</label> <label>Alto:</label><input name=alto type:number min=0 value=100><label>cm</label>";
  126.                 document.getElementById("tabla").rows[1].cells[6].innerHTML =datos[0];
  127.             };
  128.         });
  129.  
  130.     });
  131.    
  132.  
  133.  
  134. });
  135.  
  136.  
  137.  
  138.    
  139.  
  140.  </script>
  141. </head>
  142.  
  143.  
  144.     <section id="contenedor">
  145.             <aside id="menu"><H1>Cotizacion</H1></aside>
  146.            
  147.             <?php
  148.  
  149.                         echo "<table id=tabla>";
  150.                         echo "<thead>";
  151.                         echo "<tr>";
  152.                             echo "<th></th>";
  153.                             echo "<th>Nombre</th>";
  154.                             echo "<th>Precio</th>";
  155.                             echo "<th>Unidad de medida</th>";
  156.                             echo "<th>Medida</th>";
  157.                             echo "<th>Cantidad</th>";
  158.                             echo "<th>Subtotal</th>";
  159.                             echo "<th></th>";
  160.                         echo "</tr>";
  161.                         echo "</thead>";
  162.                         echo "<tbody>";
  163.                         /*echo "<tr class=fila-base>";
  164.                             $i=1;
  165.                         echo "<td>1</td>";
  166.                         echo "<td><select name=nombre required class=producto>  ";
  167.                         echo "<option value=0>Elegir una opciones</option>";
  168.                         while( $fila = mysql_fetch_row( $res ) ){
  169.                                 echo "<option value=".$i.">".$fila[1]."</option>";
  170.                             $i++;
  171.                         }
  172.                         echo "</select></td>";
  173.                         $j=1;
  174.                         $sql1 = " SELECT * from productos ";
  175.                         $res1 = mysql_query( $sql1, $h ) or die( "ERROR al ejecutar instruccion SQL." );
  176.                         echo "<td><select name=precio >  ";
  177.                         echo "<option value=0>Elegir una opciones</option>";
  178.  
  179.                         while( $fila1 = mysql_fetch_row( $res1 ) ){
  180.                                 echo "<option value=".$j.">".$fila1[2]."</option>";
  181.                             $j++;
  182.                         }
  183.                         echo "</select></td>";
  184.                         echo "<td><label>unidad de medida</label></td>";
  185.                         echo "<td><label>medida</label></td>";
  186.                         echo "<td><input type=number min=1 class=cantidad /></td>";
  187.                         echo "<td><label>0</label></td>";
  188.                         echo "<td class=eliminar>Eliminar</td>";
  189.                         echo "</tr>";*/
  190.  
  191.                         echo "</tbody>";
  192.                         echo "</table>";
  193.                
  194.             ?> 
  195.        
  196.             <!--<input type="button" id="agregar" value="Agregar fila" />-->
  197.     </section>
  198.     <input type="text" class="unidad" />
  199. </body>
  200. </html>


nose que cambia que en uno funciona y en el otro no

de ante manos muchas gracias si me pueden ayudar

Última edición por derleguan; 23/10/2014 a las 14:28 Razón: error codigo
  #2 (permalink)  
Antiguo 23/10/2014, 16:18
Avatar de derleguan  
Fecha de Ingreso: abril-2012
Mensajes: 26
Antigüedad: 12 años
Puntos: 1
Respuesta: validar input solo numeros de un tr en jquery

si bien aun no encuentro como arreglar el problema vi que el metodo on de jquery sirve para agregar eventos a elementos agregados dinamicamente
  #3 (permalink)  
Antiguo 25/10/2014, 13:38
Avatar de derleguan  
Fecha de Ingreso: abril-2012
Mensajes: 26
Antigüedad: 12 años
Puntos: 1
Respuesta: validar input solo numeros de un tr en jquery

$(document).on('change', '.unidad', function() {});

ahi ta la respuesta para los que le salga la misma duda.

Etiquetas: html, javascript
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 03:18.