Javascript 检测浏览器自动完成值的选择
我有一个带有 onkeyup
事件的文本字段。但是当我选择浏览器自动完成值时,不会触发此事件。我已经添加了 onclick 事件,但它不起作用。
我已经测试了 stackoverflow 上发布的许多解决方案来捕获浏览 autocoComplete 选择,但没有任何解决方案可以解决此问题。
尝试这个简单的示例来查看问题(在 Firefox 3.6、Chrome 10.0 和 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>
谢谢!
I have a text field with an onkeyup
event. But this event is not fired when I select a browser autoComplete value. I have added an onclick event, but it doesn't work.
I have tested many solutions posted on stackoverflow for catching the browse autocoComplete selection, but nothing has resolved this problem.
Try this simple example to see the issue (reproduced on Firefox 3.6, Chrome 10.0 and 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>
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
较新的浏览器支持 oninput 事件:
如果您需要支持较旧的浏览器,请尝试
Newer browsers support the oninput event:
If you need to support older browsers, try