Html 启用浏览器的表单自动填充

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8715000/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 12:20:06  来源:igfitidea点击:

Enabling browser's form auto-filling

htmlformsgoogle-chrome

提问by Rocket Hazmat

I am making a signup form for my website. It contains various standard form fields. Such as: name, address, phone number, user name, password, etc.

我正在为我的网站制作注册表单。它包含各种标准表单字段。如:姓名、地址、电话号码、用户名、密码等。

When I double-click on a field (in Chrome 16), so I can auto-fill my address, I get this message:

当我双击一个字段(在 Chrome 16 中)以便我可以自动填写我的地址时,我收到以下消息:

This webpage has disabled automatic filling for this form.

此网页已禁用此表格的自动填写。

I didn't disable it, so how do I enable it? I tried adding autocomplete="on"to the <form>tag, and to (some of) the <input>tags, and that didn't help.

我没有禁用它,那么如何启用它?我尝试添加autocomplete="on"<form>标签和(一些)<input>标签,但没有帮助。

Do I need to add autocomplete="on"to every field? Also, how does the browser know what field is what? Do I need to name the fields something special?

我需要添加autocomplete="on"到每个字段吗?另外,浏览器如何知道哪个字段是什么?我需要为字段命名一些特殊的东西吗?

Another question: Is there some kind of onautocompleteevent that gets triggered when a form is auto-filled? On my form, when you enter a zip code, it looks in our database (via AJAX) and then gets the state and city (city and state are dropdowns, because zip codes can be for more than one city), and fills them in for you. I was hoping I could have that run after the form was auto-filled.

另一个问题:onautocomplete自动填写表单时是否会触发某种事件?在我的表单上,当您输入邮政编码时,它会在我们的数据库中查找(通过 AJAX),然后获取州和城市(城市和州是下拉列表,因为邮政编码可以用于多个城市),然后填写它们为你。我希望我可以在自动填写表格后运行它。

P.S. I'm using the jQuery form validationplugin, if that matters.

PS 我正在使用 jQuery表单验证插件,如果这很重要的话。

回答by Rocket Hazmat

The problem was that the form tag didn't have method="POST"on it.

问题是表单标签上没有method="POST"

After Googling the message, I found a bug report, and one of the commentsmentioned method="POST".

谷歌搜索消息后,我发现了一个错误报告,以及提到的评论之一method="POST"

I added method="POST", and voila! Auto-fill works.

我加了method="POST",瞧!自动填充工作。

In some cases you may also need to add an actionif the form doesn't have one. action="javascript:void(0)"works.

在某些情况下,action如果表单没有,您可能还需要添加一个。action="javascript:void(0)"作品。

Note: Auto-fill seems to trigger the onchangeevent.

注意:自动填充似乎会触发该onchange事件。

Note 2: As for how the browser knows what field is what, see this question: How to trigger Autofill in Google Chrome?

注2:至于浏览器如何知道什么是什么字段,请看这个问题:如何在谷歌浏览器中触发自动填充?

回答by canihavesomecoffee

The option to turn off autcomplete is normally located in the form tag, see the Developer page from Mozilla here. This should mean that normally removing that attribute should enable it again on a webpage.

关闭自动完成功能的选项通常位于表单标签中,请在此处查看 Mozilla 的开发人员页面。这应该意味着通常删除该属性应该在网页上再次启用它。

Concerning the second part with the AJAX request, I don't think there's a listener for that, but you could add a function that checks the value of the field each x seconds for example, and if it changed you can perform your lookup.

关于 AJAX 请求的第二部分,我认为没有侦听器,但是您可以添加一个函数,例如每 x 秒检查一次字段的值,如果它发生变化,您可以执行查找。