如何通过代码有选择地禁用浏览器文本输入中的自动填充? [英] How to disable the autofill in browser text inputs selectively via code?

查看:27
本文介绍了如何通过代码有选择地禁用浏览器文本输入中的自动填充?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用代码有选择地禁用文本字段中的自动填充功能?

我正在 ASP.Net AJAX 中开发我的自定义代码以在数据库中搜索建议,我想防止在用户开始在文本框中键入时出现浏览器建议.

我正在寻找一种适用于大多数现代浏览器(IE 7 和 8、Firefox、Safari 和 Chrome)的解决方案.如果解决方法是在 Javascript 中就可以了.

解决方案

查看自动完成 HTML 属性(在表单或输入标签上).

[...] </form>

W3C > 自动完成属性

现在 - 从评论来看 - 网络浏览器并不总是尊重自动完成标记定义的行为.这种不尊重可能会被一些 JavaScript 代码包围,但在使用它之前你应该考虑你想要做什么.

首先,字段会在页面加载过程中被填充,并且只有在页面加载后才会被清空.用户会质疑为什么该字段已被清空.

其次,这将重置其他网络浏览器机制,例如当您返回上一页时自动填充字段.

jQuery( function(){$("输入[自动完成=关闭]").val("");});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><form method="post" action=""><div class="form-group"><label for="username">用户名</label><input type="text" id="username" name="username" value="John" autocomplete="off">

<div class="form-group"><label for="password">密码</label><input type="password" id="password" name="password" value="qwerty" autocomplete="off">

<input type="submit" value="登录">

Is it possible to selectively disable the autofill feature in text fields using code?

I'm developing my custom code in ASP.Net AJAX to search for the suggestion in the database and I would like to prevent the browser suggestions from appearing when the user starts typing in the textbox.

I'm looking for a solution that works in the most modern browsers (IE 7 & 8, Firefox, Safari and Chrome). It's is ok if the workaround is in Javascript.

解决方案

Look at the autocomplete HTML attribute (on form or input tags).

<form [...] autocomplete="off"> [...] </form>

W3C > Autocomplete attribute

Edit:

Nowadays - from the comments - web browsers do not always respect the autocomplete tag defined behavior. This non-respect could be surrounded with a little of JavaScript code but you should think about you want to do before use this.

First, fields will be filled during the page loading and will only be emptied once the page load. Users will question why the field has been emptied.

Second, this will reset other web browser mechanisms, like the autofill of a field when you go back to the previous page.

jQuery( function()
{
  $("input[autocomplete=off]").val( "" );
} );

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form method="post" action="">
  <div class="form-group">
    <label for="username">Username</label>
    <input type="text" id="username" name="username" value="John" autocomplete="off">
  </div>
  <div class="form-group">
    <label for="password">Password</label>
    <input type="password" id="password" name="password" value="qwerty" autocomplete="off">
  </div>
  <input type="submit" value="Login">
</form>

这篇关于如何通过代码有选择地禁用浏览器文本输入中的自动填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
C#/.NET最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆