Ver Mensaje Individual
  #6 (permalink)  
Antiguo 24/10/2012, 11:26
Avatar de marlanga
marlanga
 
Fecha de Ingreso: enero-2011
Ubicación: Murcia
Mensajes: 1.024
Antigüedad: 13 años, 4 meses
Puntos: 206
Respuesta: Dos Funciones de JavaScript

Eso del doctype está muy bien si quieres pasar el 3wc validator, pero si lo que de verdad quieres es que funcione tu código, mira estos cambios:

Código HTML:
Ver original
  1.     <head>
  2.       <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
  3.         <meta charset="utf-8" />    
  4.         <title>Ejemplo</title>  
  5.         <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.0/themes/base/jquery-ui.css" />  
  6.         <script src="http://code.jquery.com/jquery-1.8.2.js"></script>
  7.         <script src="http://code.jquery.com/ui/1.9.0/jquery-ui.js"></script>  
  8.         <link rel="stylesheet" href="/resources/demos/style.css" />  
  9.           <script>    
  10.           var availableTags = [    
  11.           "ActionScript",      
  12.           "AppleScript",    
  13.           "Asp",      
  14.           "BASIC",      
  15.           "C",      
  16.           "C++",    
  17.           "Clojure",    
  18.           "COBOL",      
  19.           "ColdFusion",
  20.           "Daniel",
  21.           "Mary",  
  22.           "Erlang",        
  23.           "Fortran",    
  24.           "Groovy",    
  25.           "Haskell",  
  26.           "Java",    
  27.           "Berletzis",
  28.           "Xml",
  29.           "web",
  30.           "Lisp",    
  31.           "Perl",      
  32.           "PHP",      
  33.           "Python",  
  34.           "Ruby",    
  35.           "Scala",      
  36.           "Scheme"  
  37.           ];  
  38.           function split( val ) {          
  39.           return val.split( /,\s*/ );        }    
  40.           function extractLast( term ) {        
  41.           return split( term ).pop();      
  42.           }
  43.             function focusear(nodo){
  44.             nodo.bind( "keydown", function( event ) {          
  45.               if ( event.keyCode === $.ui.keyCode.TAB &&      
  46.               $( this ).data( "autocomplete" ).menu.active ) {  
  47.               event.preventDefault();                }            }).
  48.               autocomplete({                minLength: 0,              
  49.               source: function( request, response ) {                
  50.               // delegate back to autocomplete, but extract the last term    
  51.               response( $.ui.autocomplete.filter(            
  52.               availableTags, extractLast( request.term ) ) );                },      
  53.               focus: function() {                
  54.               // prevent value inserted on focus          
  55.               return false;                },        
  56.               select: function( event, ui ) {              
  57.               var terms = split( this.value );  
  58.               // remove the current input                
  59.               terms.pop();                
  60.               // add the selected item            
  61.               terms.push( ui.item.value );            
  62.               // add placeholder to get the comma-and-space at the end  
  63.               terms.push( "" );                    this.value = terms.join( ", " );    
  64.               return false;                }            });
  65.              
  66.           }
  67.            
  68.  
  69.         $(function() {
  70.             $('#btnAdd').click(function() {
  71.                 var num     = $('.name').length; // how many "duplicatable" input fields we currently have
  72.                 var newNum  = new Number(num + 1);      // the numeric ID of the new input field being added
  73.  
  74.                 // create the new element via clone(), and manipulate it's ID using newNum value
  75.                 var newElem = $('<input type="text" class="text name" name="name" value="" />').attr('id', 'name' + newNum).attr('name', 'name' + newNum);
  76.  
  77.                 // insert the new element after the last "duplicatable" input field
  78.                 $('#name' + num).after(newElem);
  79.  
  80.                 // enable the "remove" button
  81.                 $('#btnDel').removeAttr('disabled');
  82.  
  83.                 // business rule: you can only add 10 names
  84.                 if (newNum == 10)
  85.                     $('#btnAdd').attr('disabled','disabled');
  86.                    
  87.                 focusear(newElem);
  88.             });
  89.  
  90.             $('#btnDel').click(function() {
  91.                 var num = $('.name').length; // how many "duplicatable" input fields we currently have
  92.                 $('#name' + num).remove();     // remove the last element
  93.  
  94.                 // enable the "add" button
  95.                 $('#btnAdd').removeAttr('disabled');
  96.  
  97.                 // if only one element remains, disable the "remove" button
  98.                 if (num == 2)
  99.                     $('#btnDel').attr('disabled','disabled');
  100.             });
  101.  
  102.             $('#btnDel').attr('disabled','disabled');
  103.            
  104.             focusear($("#name1"));  
  105.         });
  106.     </script>
  107.          
  108.     </head>
  109.  
  110.  
  111. <form id="myForm">
  112.     <div id="input1" style="margin-bottom:4px;" class="clonedInput">
  113.         Nombre:<p class="ui-widget"><label for="name"> <input type="text" class="text name" name="name1" value="" id="name1" /></label>
  114.     <div>
  115.         <input type="button" id="btnAdd" value="Agregar otro nombre" />
  116.         <input type="button" id="btnDel" value="Eliminar nombre" />
  117.     </div>
  118. </form>
  119.  
  120. </body>
  121. </html>

No eliminabas bien el atributo "disabled" de los elementos.
Al clonar los elementos, además de clonar los botones innecesariamente, también clonabas los métodos del "autocomplementar", que darán problemas (por ejemplo, el FOCUS siempre te manda al focus del primer elemento, ya que has clonado sus métodos y eventos). Lo que hay que hacer es convertir el input:text en un autocompletar cada vez que se cree. No solo al principio.
Y otros fallos menores como tener separado código en común en dos scripts diferentes con sus propios ámbitos.