Javascript:防止浏览器在按下向下键时显示字段的输入历史记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17670190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript: Prevent browser to display input history for a field on press of down key
提问by Ram
Is there anyway to prevent the browser from displaying previously inputted values for a field on press of the down key?
有没有办法阻止浏览器在按下向下键时显示先前输入的字段值?
If this is not possible, another problem of mine is on press of the down key, the list of previously inputted values will be shown and on press of the TAB key, the currently highlighted value on the list will be chosen and will be set as the value of the field. I do not want this, I just want the focus to be passed to the next input element w/o choosing any values.
如果这是不可能的,我的另一个问题是按下向下键,将显示先前输入的值列表,按下 TAB 键,将选择列表中当前突出显示的值并将其设置为字段的值。我不想要这个,我只想将焦点传递到下一个输入元素,而无需选择任何值。
Are there any ways to override the browser behavior? A solution in play javascript is preferred though jQuery solutions are fine as well. Thanks!
有什么方法可以覆盖浏览器行为吗?尽管 jQuery 解决方案也很好,但首选 javascript 中的解决方案。谢谢!
回答by adeneo
Just add the autocomplete attribute
只需添加自动完成属性
<input type="text" autocomplete="off" />
回答by CodingIntrigue
You can disable autocomplete by adding autocomplete="off"
onto your input tags. MDN Reference
您可以通过添加autocomplete="off"
到您的输入标签来禁用自动完成。MDN 参考
Example
例子
<input type="text" name="email" value="" autocomplete="off" />
See this Stack Overflow postfor browser compatibility.
有关浏览器兼容性,请参阅此Stack Overflow 帖子。
回答by Saqib khan
html code
html代码
<input type="text" autocomplete="off" id="password-field" class="form-control" name="multi_user_pin" placeholder="Type your PIN here"/>
jquery code
jquery code
$('#password-field').on('input keydown',function(e) {
if(e.keyCode == 8 && $(this).val().length==1) {
$(this).attr('type', 'text');
$(this).val('');
}
else{
if ($(this).val() !== '') {
$(this).attr('type', 'password');
} else {
$(this).attr('type', 'text');
}
}
});
This code works fine for password field to prevent to remember its history with all browsers
此代码适用于密码字段,以防止在所有浏览器中记住其历史记录