浏览器本机自动完成动态生成的表单(使用 ajax 处理)
我正在动态生成一个表单。为了简单起见,假设它是一个带有电子邮件/密码的登录表单。表单已提交,但 onsubmit 会触发处理实际登录的 AJAX 请求,并且提交事件会被取消 (e.preventDefault()
)。
我使用 e.preventDefault()
取消表单的默认操作,即转到“action”中的页面,但这似乎也取消了自动完成检测浏览器的。
我认为您需要满足本机自动完成功能的几个要求:
- 您的输入字段
type="text"
必须有一个name
- 必须提交表单< ;-- 这在我的情况下并没有真正发生
我的分析正确吗?有什么方法可以让自动完成在这种情况下工作吗?
为了避开粉丝:我不是在寻找任何涉及 jQuery 或 [插入你的框架] 的解决方案,我想使用本机浏览器自动完成功能。我没有想要自动完成的单词列表。
I am dynamically generating a form. For simplicity's sake assume it's a login form with email/password. The form is submitted, but onsubmit it fires an AJAX request that handles the actual login and the submit event is cancelled (e.preventDefault()
).
I use e.preventDefault()
to cancel the default action of the form, that is, go to the page in 'action' but this also seems to cancel the autocomplete detection of the browser.
I think you need to fullfill several requirements for the native autocomplete to work:
- Your input field
type="text"
must have aname
- The form must be submitted <-- this isn't really happening in my case
Is my analysis correct and is there any way to make autocomplete work in this case?
To ward off the fanboys: I'm not looking for any solution that involves jQuery or [insert your framework], I want to use the native browser autocomplete feature. I don't have a list of words that I want to autocomplete with.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的,我找到了一种复杂的方法来执行此操作:
这是 javascript:
这是 html:
单击提交将运行 ajaxit() 方法。该方法在 iframe 中创建相同的表单,并将其提交到服务器。您可以搭载提交来执行服务器请求,也可以执行单独的 ajax 请求并忽略 iframe。
我知道这很丑陋,但它确实有效。
编辑:这是一个可以使用的 jsbin 。
Ok i found a convoluted way to do this:
Here is the javascript:
Here is the html:
Clicking submit will run the ajaxit() method. the method creates the same form in an iframe, and submits it to the server. You can either piggyback that submit to do your server request or you can do a separate ajax request and ignore the iframe.
I know that it's ugly, but it works.
EDIT: Here is a jsbin to play with.
DMoses 解决方案极大地启发了我的解决方案,但它有一个显着的差异,所以我认为制定自己的解决方案是个好主意,赏金归 DMoses 虽然 :P
DMoses 解决方案将表单移动(或复制)到 iframe,然后提交它。您想要执行此操作的原因是您的“父级”不会重新加载。有一个更简单的解决方案:将表单提交到 iframe。这工作起来是一样的,您不必复制任何节点。
此步骤也完全可重复。唯一的缺点是您无法控制何时添加自动完成条目。您可能只想添加有效条目,但至少这种情况完美地模仿了不涉及 ajax 时正常表单的行为方式。如果您想控制添加到自动完成中的内容,请使用 DMoses 的解决方案,复制表单并在 iframe 中提交。
对我来说,这已经足够了:
最好的部分:不需要额外的 JavaScript 来完成这项工作!(除了为表单生成唯一的 id/名称之外,但这非常简单)。
jsFiddle: http://jsfiddle.net/KzF6s/13/
DMoses solution greatly inspired my solution but it there is a significant difference so I thought it would be a good idea to make my own solution, the bounty goes to DMoses though :P
DMoses solution moves (or copies) the form to the iframe and then submits it. The reason you want to do this is so your 'parent' from doesn't reload. There is a simpler solution: have the form submit to the iframe. This works just the same and you don't have to copy any nodes.
This step is entirely repeatable as well. The only downside is that you don't control when exactly an autocomplete entry is added. You might want to add only valid entries but at least this case mimics perfectly the way a normal form would behave if no ajax were involved. If you want to control what gets added to the autocomplete, use DMoses' solution, copy the form and submit it in the iframe.
For me, this is enough:
The best part: no extra JavaScript is required to make this work! (other than generating a unique id/name for the form, but that's super trivial).
jsFiddle: http://jsfiddle.net/KzF6s/13/
在对其他答案的解决方法的评论中进行了一些修改之后,我决定为任何可以等待浏览器正确处理此问题的人制作此列表。
因此,总而言之,这些浏览器版本支持使用 JavaScript 生成的表单上的自动完成值:
After messing around a bit with fiddles in comments on other answers' workarounds, I decided to make this list for anyone who can wait for browsers to handle this correctly.
So, as a summary, these browser versions support autocomplete values on forms generated with JavaScript: