Advertisement
Oscargm

Untitled

Nov 28th, 2014
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 4.06 KB | None | 0 0
  1. <!DOCTYPE html >
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" >
  5. <title>Ejercicio 19 - Autocompletar</title>
  6.     <script type="text/javascript">
  7.         var sugerencias = null;
  8.         var elementoSeleccionado=-1;
  9.         var sugerencia;
  10.         function inicializa_xhr() {
  11.             if (window.XMLHttpRequest) {
  12.                 return new XMLHttpRequest();
  13.             } else if (window.ActiveXObject) {
  14.                 return new ActiveXObject("Microsoft.XMLHTTP");
  15.             }
  16.         }
  17.        
  18.         function autocompleta(e) {
  19.             var tecla = e.keyCode;
  20.             switch (tecla ){
  21.                 case 40:// Flecha Abajo
  22.                     if(elementoSeleccionado<sugerencia.length){
  23.                         elementoSeleccionado++;
  24.                         actualizaSugerencias();
  25.                     }
  26.                    
  27.                     break;
  28.                 case 38:// Flecha Arriba
  29.                
  30.                     if(elementoSeleccionado>0){
  31.                         elementoSeleccionado--;
  32.                         actualizaSugerencias();
  33.                     }
  34.                            
  35.                     break;
  36.                 case 39: // ENTER o Intro
  37.                     seleccionaElemento();              
  38.                     break;
  39.                 default:
  40.                     // lanzar peticion al php
  41.                     var muni = document.getElementById("municipio").value;
  42.                     if(muni==""){
  43.                         borraLista();
  44.                     }else{
  45.                     peticion = inicializa_xhr();
  46.        
  47.                     if (peticion) {
  48.                         peticion.onreadystatechange = muestraInformacion;
  49.                         peticion.open("POST","autocompletaMunicipios.php?nocache=" + Math.random(),true);
  50.                         peticion.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  51.                         peticion.send("municipio="+muni);
  52.                     }
  53.                         }
  54.  
  55.             }
  56.            
  57.         }
  58.         function muestraInformacion() {
  59.             //llamar a sinresultados si no devuelve nada
  60.             //llamar a actualizaSugerencias si devuelve algo
  61.            
  62.             if (peticion.readyState == 4) {
  63.                 if (peticion.status == 200) {
  64.                     var lista = document.getElementById("municipio");
  65.                     sugerencia = JSON.parse(peticion.responseText);
  66.                    
  67.                     if (sugerencia.length>0){
  68.                         actualizaSugerencias();
  69.                         /*document.getElementById("sugerencias").innerHTML=sugerencia.formateaLista();
  70.                         document.getElementById("sugerencias").style.display="block";*/
  71.                     }
  72.                     else {
  73.                         sinResultados();
  74.                     }
  75.                 }
  76.             }
  77.  
  78.         }
  79.         function actualizaSugerencias() { //0          
  80.             //document.getElementById("sugerencias").innerHTML=formateaLista(sugerencia);
  81.             document.getElementById("sugerencias").innerHTML=sugerencia.formateaLista();
  82.             document.getElementById("sugerencias").style.display="block";
  83.         }
  84.         function sinResultados(){
  85.             document.getElementById("sugerencias").innerHTML="No hay resultados.";
  86.             document.getElementById("sugerencias").style.display="block";
  87.         }
  88.         function seleccionaElemento(){
  89.         }
  90.         function borraLista() {
  91.             document.getElementById("sugerencias").innerHTML="";
  92.             document.getElementById("sugerencias").style.display="none";
  93.         }
  94.        
  95.     /*   function formateaLista( tabla) {
  96.             var cadena="<ul>";
  97.             var i;
  98.             for(i=0;i<sugerencias.length;i++){
  99.                 if (i==elementoSeleccionado){
  100.                     cadena+="<li class='seleccionado'>";               
  101.                 }else{
  102.                     cadena+="<li>";
  103.                 }
  104.                 cadena+=sugerencias[i];
  105.                 cadena+="</li>";               
  106.             }
  107.             cadena+="</ul>"
  108.             return cadena;
  109.         }
  110. */
  111.         Array.prototype.formateaLista = function() {
  112.             var cadena="<ul>";
  113.             var i;
  114.             for(i=0;i<this.length;i++){
  115.                 if (i==elementoSeleccionado){
  116.                     cadena+="<li class='seleccionado'>";               
  117.                 }else{
  118.                     cadena+="<li>";
  119.                 }
  120.                 cadena+=this[i];
  121.                 cadena+="</li>";               
  122.             }
  123.             cadena+="</ul>"
  124.             return cadena;
  125.         }
  126.        
  127.     </script>
  128.     <style type="text/css">
  129.         *{
  130.             margin: 0; padding: 0;
  131.         }
  132.         body {font-family: Arial, Helvetica, sans-serif;}
  133.         #sugerencias {display:none;width:200px; margin-left: 83px; border:1px solid black; }
  134.         #sugerencias ul { font-size:.85em; list-style: none;}
  135.         #sugerencias ul li {padding: .2em; border-bottom: 1px solid silver;}
  136.         .seleccionado {font-weight:bold; background-color: #FFFF00;}
  137.        
  138.     </style>
  139.  
  140. </head>
  141.  
  142. <body>
  143.     <h1>Autocompletar texto</h1>
  144.    
  145.     <form>
  146.       <label for="municipio">Municipio</label> &nbsp;&nbsp;
  147.       <input type="text" id="municipio" name="municipio" size="30" onkeyup="autocompleta(event);" >
  148.     </form>
  149.     <div id="sugerencias">
  150.     </div>
  151. </body>
  152. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement