浏览器自动填充和 Javascript 触发事件
我们的一位用户刚刚提出了这样一个事实:他们的浏览器自动填充不会导致 JS onChange 事件触发;这会导致我们的用户注册出现问题。
这是设计使然吗?有办法解决吗?
One of our users just brought up the fact that their browsers Autofill doesn't cause JS onChange events to fire; this causes a problem with user registration for us.
Is this by design? Is there a way to work around it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我偶尔使用的一种解决方案是检查字段/输入/选择的值是否与其默认值不同。 defaultValue 是标记中最初的值,value 是当前值,也称为选择或输入的值。即使表单是自动填充的,这也可能会有所不同。
如果您想完全关闭自动填充,添加
autocomplete="off"
可能是明智之举与您的逻辑直接相关的字段。
One solution I have been using occasionally is to check whether the value of the field/input/select differs from it's defaultValue. defaultValue would be the value that was originally in the markup, and value is the current value aka selected or entered value. This would probably differ even though the form was autopopulated.
If you want to turn off autofill altogether, it might be wise to add
autocomplete="off"
on fields that are directly connected to your logic.
如果您想获得自动填充行为但更改样式,也许您可以执行以下操作(
jQuery
):If you want to get the autofill behaviour but change the styling, maybe you can do something like this (
jQuery
):您是否尝试过使用 onpropertychanged 而不是 onchange 事件?但这仅适用于 IE,并且是 MSDN 上推荐的修复方法。
Have you tried using the onpropertychanged instead of onchange event? That's for IE only though and is the recommended fix on MSDN.
以防万一有人仍在寻找解决方案(就像我今天一样),监听浏览器自动填充更改,这是我构建的自定义 jquery 方法,只是为了简化向输入添加更改侦听器时的过程:
你可以这样称呼它:
Just in case someone is still looking for a solution (just as I was today), to listen to a browser autofill change, here's a custom jquery method that I've built, just to simplify the proccess when adding a change listener to an input:
You can call it like this:
这是一个非常好的解决方案,其功能与 jishi 所描述的类似:
https://web.archive.org/web/20131125153914/http://furrybrains.com/2009/01/02/capturing-autofill-as- a-change-event/
使用 Wayback Machine 链接更新了损坏的链接
Here's a pretty good solution that does something similar to what jishi described:
https://web.archive.org/web/20131125153914/http://furrybrains.com/2009/01/02/capturing-autofill-as-a-change-event/
Updated the broken link with Wayback Machine link