简体   繁体   English

如何禁用特定输入字段的自动填充

[英]how to disable autofill for specific input field

我正在使用输入字段使用 google api 获取邮政编码,我尝试使用 autocomplete="off" 停止自动填充,但仍然有以前的输入

 <input  onClick="this.select();" type="text" id="postal_code" name="postal_code" autocomplete="off" onFocus="geolocate()">

You are using autocomplete attribute in the input fields.您在输入字段中使用autocomplete属性。 You should be using that in the <form> tag to toggle the autocomplete.您应该在<form>标签中使用它来切换自动完成。

 <form action="#" method="get" autocomplete="off"> First name:<input type="text" name="fname"><br> </form> <br> <br> <form action="#" method="get" autocomplete="on"> First name:<input type="text" name="fname"><br> </form>

  1. For sure you can use autocomplete in input tags.当然,您可以在输入标签中使用自动完成功能。
  2. And its not right to add couple of form tags like this.像这样添加几个表单标签是不对的。
  3. If you can show the full form code...if possoble如果您可以显示完整的表单代码...如果可能

Here's the perfect solution that will work in all browsers!这是适用于所有浏览器的完美解决方案!

TL;DR TL; 博士

Rename your input field names and field ids to something non-related like 'data_input_field_1' .将您的输入字段名称和字段 ID 重命名为不相关的名称,例如'data_input_field_1' Then add the &#8204;然后添加&#8204; character into the middle of your labels.字符到标签的中间。 This is a non-printing character, so you won't see it, but it tricks the browser into not recognizing the field as one needing auto-completing, thus no built-in auto-complete widget is shown!这是一个非打印字符,因此您不会看到它,但它会诱使浏览器不将该字段识别为需要自动完成的字段,因此不会显示内置的自动完成小部件!

The Details细节

Almost all browsers use a combination of the field's name, id, placeholder, and label to determine if the field belongs to a group of address fields that could benefit from auto-completion.几乎所有浏览器都使用字段名称、ID、占位符和标签的组合来确定该字段是否属于可以从自动完成中受益的一组地址字段。 So if you have a field like <input type="text" id="address" name="street_address"> pretty much all browsers will interpret the field as being an address field.因此,如果您有一个像<input type="text" id="address" name="street_address">的字段,几乎所有浏览器都会将该字段解释为地址字段。 As such the browser will display its built-in auto-completion widget.因此,浏览器将显示其内置的自动完成小部件。 The dream would be that using the attribute autocomplete="off" would work, unfortunately, most browsers nowadays don't obey the request.梦想是使用属性autocomplete="off"会起作用,不幸的是,现在大多数浏览器不遵守请求。

So we need to use some trickery to get the browsers to not display the built-in autocomplete widget.所以我们需要使用一些技巧来让浏览器不显示内置的自动完成小部件。 The way we will do that is by fooling the browser into believing that the field is not an address field at all.我们这样做的方法是让浏览器相信该字段根本不是地址字段。

Start by renaming the id and the name attributes to something that won't give away that you're dealing with address-related data.首先将idname属性重命名为不会泄露您正在处理与地址相关的数据的内容。 So rather than using <input type="text" id="city-input" name="city"> , use something like this instead <input type="text" id="input-field-3" name="data_input_field_3"> .因此,不要使用<input type="text" id="city-input" name="city"> ,而是使用类似这样的东西<input type="text" id="input-field-3" name="data_input_field_3"> The browser doesn't know what data_input_field_3 represents.浏览器不知道 data_input_field_3 代表什么。 But you do.但你做了。

If possible, don't use placeholder text as most browsers will also take that into account.如果可能,不要使用占位符文本,因为大多数浏览器也会考虑到这一点。 If you have to use placeholder text, then you'll have to get creative and make sure you're not using any words relating to the address parameter itself (like City ).如果您必须使用占位符文本,那么您必须发挥创意并确保您没有使用任何与地址参数本身相关的词(例如City )。 Using something like Enter location can do the trick.使用诸如Enter location类的东西可以解决问题。

The final parameter is the label attached to the field.最后一个参数是附加到字段的标签 However, if you're like me, you probably want to keep the label intact and display recognizable fields to your users like "Address", "City", "State", "Country".但是,如果您像我一样,您可能希望保持标签不变,并向您的用户显示可识别的字段,例如“地址”、“城市”、“州”、“国家”。 Well, great news: YOU CAN!好吧,好消息:你可以! The best way to achieve that is to insert a Zero-Width Non-Joiner Character &#8204;实现这一目标的最佳方法是插入一个零宽度非连接符&#8204; as the second character in the label.作为标签中的第二个字符。 So replacing <label>City</label> with <label>C&#8204;ity</label> .所以用<label>City</label> <label>C&#8204;ity</label>替换<label>City</label> <label>C&#8204;ity</label> This is a non-printing character, so your users will see City , but the browser will be tricked into seeing C ity and not recognize the field!这是一个非打印字符,因此您的用户将看到City ,但浏览器将被欺骗看到C ity而无法识别该字段!

Mission accomplished!任务完成! If all went well, the browser should not display the built-in address auto-completion widget on those fields anymore!如果一切顺利,浏览器不应再在这些字段上显示内置地址自动完成小部件!

Hope this helps you in your endeavors!希望这对您的努力有所帮助!

Try adding this to your input tag: autocomplete="__away"尝试将此添加到您的input标签: autocomplete="__away"

Chrome somehow does not follow the autocomplete="off" directive, and so we have to use this workaround doing an undefined value. Chrome 不知何故不遵循autocomplete="off"指令,因此我们必须使用此解决方法来处理未定义的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM