Ver Mensaje Individual
  #12 (permalink)  
Antiguo 23/03/2012, 17:04
Avatar de IsaBelM
IsaBelM
Colaborador
 
Fecha de Ingreso: junio-2008
Mensajes: 5.032
Antigüedad: 15 años, 11 meses
Puntos: 1012
Respuesta: Utilizar las teclas direccionales para seleccionar un item

prueba ahora
Código Javascript:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml"lang="es" xml:lang="es">
  3. <head>
  4. <meta name="http-equiv" content="Content-type: text/html; charset=utf-8"/>
  5. <style type="text/css">
  6. * {
  7.     padding: 0;
  8.     margin: 0;
  9.     position: relative;
  10. }
  11.  
  12. input.autocomplete {
  13.     width: 200px;
  14.     font: 1em Verdana;
  15. }
  16.  
  17.  
  18.  
  19. #contenedorOpt {
  20.     min-width: 200px;
  21.     width: auto;
  22.     display: none;
  23.     background: rgb(255, 255, 255);
  24.     border: 1px solid rgb(120,120,120);
  25.     text-align: left;
  26.     position: absolute;
  27.     z-index: 100;
  28. }
  29.  
  30.  
  31.  
  32. #contenedorOpt {
  33.     list-style-type: none;
  34. }
  35.  
  36.  
  37.  
  38. #contenedorOpt li {
  39.     font: 1em verdana;
  40.     text-align: left;
  41.     padding: 4px;
  42.     margin: 1px;
  43.     border: 1px solid rgb(255, 255, 255);
  44.     color: rgb(0, 0, 0);
  45.     cursor: default;
  46. }
  47.  
  48.  
  49. #contenedorOpt li.seleccionado {
  50.     border: 1px solid rgb(95, 82, 82);
  51.     border-radius: 5px;
  52.     background: rgb(219, 215, 215);
  53.     color: rgb(9, 8, 8);
  54. }
  55. </style>
  56. <script type="text/javascript">
  57. document.addEventListener('DOMContentLoaded', function() {
  58.    
  59.     new autoCompletado();
  60.  
  61. }, false);
  62.  
  63.  
  64. function autoCompletado() {
  65.  
  66.     this.elTxtBox = null;
  67.     this.ajax = new XMLHttpRequest();
  68.     this.peticiones = 0;
  69.     this.keyUpTimeout = 0;
  70.     this.input = document.querySelectorAll('.autocomplete');
  71.     this.elDiv = document.querySelector('#contenedorOpt');
  72.     this.url = 'SelecUsuarios.php';
  73.     this.opt = null;
  74.     this.optSel = null;
  75.     var _this = this;
  76.  
  77.     Array.prototype.forEach.call(this.input, function(elem) {
  78.  
  79.         elem.addEventListener('keyup', function(event) {_this.autoCompletaPulsacion(event, this)}, false);
  80.         elem.addEventListener('paste', function() {_this.copypasteRaton(this)}, false);
  81.         elem.addEventListener('keydown', function() {_this.rellenarConEnteryTab(event)}, false);
  82.         elem.addEventListener('blur', function() {_this.Cerrar()}, false);
  83.         elem.addEventListener('cut', function() {_this.copypasteRaton(this, this.value)}, false);
  84.     });
  85.  
  86.     this.elDiv.addEventListener('click', function() {_this.rellenarConRaton()}, false);
  87. }
  88.  
  89.  
  90. autoCompletado.prototype.autoCompletaPulsacion = function(ev, elTxtBox) {
  91.  
  92.     this.elTxtBox = elTxtBox;
  93.  
  94.     if (this.elTxtBox.value.length <= 2) {
  95.  
  96.         return;
  97.     }
  98.  
  99.     var keyCode = ev.keyCode, _this = this;
  100.  
  101.     if (keyCode == 40) { // flecha abajo
  102.  
  103.         this.BajaOpt();
  104.  
  105.     } else if (keyCode == 38) { // flecha arriba
  106.  
  107.         this.SubeOpt();
  108.  
  109.     } else if (keyCode == 13) { // enter
  110.  
  111.         ev.preventDefault();
  112.  
  113.     } else {
  114.  
  115.         if (this.keyUpTimeout) clearTimeout(this.keyUpTimeout);
  116.  
  117.         this.keyUpTimeout = setTimeout(function() {
  118.  
  119.             _this.elTxtBox.insertAdjacentHTML('afterend', '<img src="./precarga.gif" />');
  120.  
  121.             _this.ajax.open('POST', _this.url, true);
  122.  
  123.             _this.ajax.onreadystatechange = function() {
  124.  
  125.                 if (_this.ajax.readyState == 4) {
  126.  
  127.                     if (_this.ajax.status == 200) {
  128.  
  129.                         _this.elDiv.innerHTML = _this.ajax.responseText;
  130.  
  131.                         _this.elTxtBox.nextElementSibling.remove();
  132.                         _this.opt = _this.elDiv.getElementsByTagName('li');
  133.  
  134.                         [].forEach.call(_this.opt, function(elem) {
  135.                                
  136.                             elem.addEventListener('mouseover',  function() {
  137.  
  138.                                 _this.Marcar(this);
  139.  
  140.                             }, false);
  141.                         });
  142.  
  143.  
  144.                         if (_this.peticiones++ == 0) {
  145.  
  146.                             var PosElemento = _this.elTxtBox.getBoundingClientRect(),
  147.                             posicionLeft = PosElemento.left,
  148.                             posicionTop = PosElemento.bottom;
  149.  
  150.                             _this.Desplieaga(posicionLeft, posicionTop);
  151.                         }
  152.  
  153.                         if (_this.elTxtBox.value.length <= 2) {
  154.  
  155.                             _this.Cerrar();
  156.                         }
  157.                     }
  158.                 }
  159.             }
  160.  
  161.             _this.ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  162.             _this.ajax.send('q='+_this.elTxtBox.value);
  163.             return;
  164.  
  165.         }, 700);
  166.     }
  167. }
  168.  
  169.  
  170.  
  171. autoCompletado.prototype.copypasteRaton = function(elTxtBox) {
  172.  
  173.     var _this = this;
  174.     setTimeout(function() {_this.autoCompletaRaton(elTxtBox)}, 0);
  175. }
  176.  
  177.  
  178. autoCompletado.prototype.autoCompletaRaton = function(elTxtBox) {
  179.  
  180.     this.elTxtBox = elTxtBox;
  181.  
  182.     if (this.elTxtBox.value.length <= 2) {
  183.  
  184.         return;
  185.     }
  186.  
  187.     var _this = this;
  188.  
  189.     this.elTxtBox.insertAdjacentHTML('afterend', '<img src="./precarga.gif" />');
  190.  
  191.     this.ajax.open('POST', _this.url, true);
  192.  
  193.     this.ajax.onreadystatechange = function() {
  194.  
  195.         if (!_this.ajax || _this.ajax == null) { return; }
  196.  
  197.         if (_this.ajax.readyState == 4) {
  198.  
  199.             if (_this.ajax.status == 200) {
  200.  
  201.                 _this.elDiv.innerHTML = _this.ajax.responseText;
  202.                
  203.                 _this.elTxtBox.nextElementSibling.remove();
  204.                 _this.opt = _this.elDiv.getElementsByTagName('li');
  205.  
  206.                 [].forEach.call(_this.opt, function(elem) {
  207.                        
  208.                     elem.addEventListener('mouseover',  function() {
  209.  
  210.                         _this.Marcar(this);
  211.  
  212.                     }, false);
  213.                 });
  214.  
  215.  
  216.                 if (_this.peticiones++ == 0) {
  217.  
  218.                     var PosElemento = _this.elTxtBox.getBoundingClientRect(),
  219.                     posicionLeft = PosElemento.left,
  220.                     posicionTop = PosElemento.bottom;
  221.  
  222.                     _this.Desplieaga(posicionLeft, posicionTop);
  223.                 }
  224.  
  225.                 if (_this.elTxtBox.value.length <= 2) {
  226.  
  227.                     _this.Cerrar();
  228.                 }  
  229.             }
  230.         }
  231.     }
  232.  
  233.     _this.ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  234.     _this.ajax.send('q='+_this.elTxtBox.value);
  235.     return;
  236. }
  237.  
  238.  
  239.  
  240. autoCompletado.prototype.Desplieaga = function(posLeft, posTop) {
  241.  
  242.     this.elDiv.style.left = posLeft + 'px';
  243.     this.elDiv.style.top = posTop + 'px';
  244.     this.elDiv.style.display = 'block';
  245. }
  246.  
  247.  
  248.  
  249.  
  250. autoCompletado.prototype.Marcar = function(sel) {
  251.  
  252.     this.optSel = sel;
  253.  
  254.     for (var i = 0; i < this.opt.length; i++) {
  255.  
  256.         if (this.opt[i].className == 'seleccionado') {
  257.  
  258.             this.opt[i].className = '';
  259.             sel.className = 'seleccionado';
  260.             break;
  261.         }
  262.     }
  263. }
  264.  
  265.  
  266.  
  267.  
  268. autoCompletado.prototype.BajaOpt = function() {
  269.  
  270.     for (var i = 0; i < this.opt.length; i++) {
  271.  
  272.         if (this.opt[i].className == 'seleccionado') {
  273.  
  274.             this.opt[i].className = '';
  275.  
  276.             i = (i < this.opt.length-1) ? ++i : 0;
  277.             this.optSel = this.opt[i];
  278.             this.opt[i].className = 'seleccionado';
  279.             break;
  280.         }
  281.     }
  282. }
  283.  
  284.  
  285.  
  286.  
  287. autoCompletado.prototype.SubeOpt = function() {
  288.  
  289.     for (var i = 0; i < this.opt.length; i++) {
  290.  
  291.         if (this.opt[i].className == 'seleccionado') {
  292.  
  293.             this.opt[i].className = '';
  294.  
  295.             i = (i < this.opt.length && i > 0) ? --i : this.opt.length-1;
  296.             this.optSel = this.opt[i];
  297.             this.opt[i].className = 'seleccionado';
  298.             break;
  299.         }
  300.     }
  301. }
  302.  
  303.  
  304.  
  305.  
  306. autoCompletado.prototype.rellenarConRaton = function() {
  307.  
  308.     this.elTxtBox.value = this.optSel.textContent;
  309.     this.Cerrar();
  310. }
  311.  
  312.  
  313.  
  314.  
  315. autoCompletado.prototype.rellenarConEnteryTab = function(ev) {
  316.  
  317.     var keyCode = ev.keyCode;
  318.  
  319.     if ((keyCode == 13) || (keyCode == 9)) {
  320.  
  321.         this.elTxtBox.value = this.optSel.textContent;
  322.         this.Cerrar();
  323.  
  324.         if (keyCode == 13) {
  325.  
  326.             ev.preventDefault();
  327.         }
  328.  
  329.     }  
  330. }
  331.  
  332.  
  333.  
  334.  
  335. autoCompletado.prototype.Cerrar = function() {
  336.  
  337.     var _this = this;
  338.  
  339.     setTimeout(function() {
  340.  
  341.         _this.elDiv.style.display = 'none';
  342.         _this.elDiv.innerHTML = '';
  343.         _this.peticiones = 0;
  344.  
  345.     }, 200);
  346.    
  347. }
  348. </script>
  349. </head>
  350. <body>
  351.  
  352. <form method="post" action="">
  353. Nombre: <input type="text" name="txt" id="txt1" class="autocomplete" value="" autocomplete="off" tabindex="1" />
  354. <br /><br />
  355. Nombre: <input type="text" name="txt" id="txt2" class="autocomplete" value="" autocomplete="off" tabindex="2" />
  356. </form>
  357.  
  358. <ul id="contenedorOpt"></ul>
  359.  
  360. </body>
  361. </html>
__________________
if(ViolenciaDeGénero) {alert('MUJER ASESINADA');}

Última edición por IsaBelM; 27/12/2016 a las 14:49 Razón: actualizar