2011-03-25 23 views
8

Tengo un campo de texto con un evento onkeyup. Pero este evento no se activa cuando selecciono un valor de autocompletar del navegador. He agregado un evento onclick, pero no funciona.Javascript detectar una selección de navegador Valor de Autocompletar

He probado muchas soluciones publicadas en stackoverflow para capturar la selección de navegación autocompleta, pero nada ha resuelto este problema.

probar este ejemplo sencillo para ver el tema (que se reproduce en Firefox 3.6, Chrome 10.0 y IE8):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html> 
<head> 
    <title>Test</title> 
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.5.1.min.js"></script> 
    <script type="text/javascript"> 
    //<![CDATA[ 
     function onkeyupInput(){ 
      $('#divResult').text($('#myInput').val()); 
     } 
    //]]> 
    </script> 
</head> 
<body> 
    <form name="myForm" action="#"> 
     Tape a value and send it. Then select this value with your browser AutoComplete value :<br /> 
     <input id="myInput" name="myInput" type="text" onkeyup="onkeyupInput();" onclick="onkeyupInput();" value="" /> 
     <input type="submit" /> 
    </form> 
    Result of onkeypress and onclick : 
    <div id="divResult"></div> 
    <br /> 
    <b>The issue : Result of onkeypress and onclick is not update when an autocomplete value of a browser is selected.</b> 
</body> 
</html> 

Gracias!

Respuesta

8

Los nuevos navegadores soportan el evento oninput:

$(function() { 
    $('#myInput').on("input",function() { 
    $('#divResult').text($(this).val()); 
    }); 
}); 

<input id="myInput" name="myInput" type="text" value="" /> 

Si tiene que soportar los navegadores antiguos, tratan

var tId = ""; 
function monitor() { 
$('#divResult').text($('#myInput').val()); 
} 

$(function() { 
    $('#myInput') 
    .focus(function() { 
     tId=setInterval(monitor,100); 
    }) 
    .blur(function() { 
     clearInterval(tId); 
    }); 
}); 
+0

Gracias, es obra! –

+0

@goby Estupendo - me alegra ayudar y aprender algo en el proceso – mplungjan

Cuestiones relacionadas