使用 Jquery 检测 Chrome 保存的输入信息?

发布于 2024-12-09 11:13:50 字数 402 浏览 7 评论 0原文

我正在使用 jquery 在输入上开发占位符文本,当输入获得焦点时,占位符文本会稍微淡出,并在用户键入时完全消失。这部分工作正常。因此,为了避免与保存的文本发生冲突,我将代码包装在:

if(!$($input).val()) {

...create placeholder, set up rules for fading it, etc...

}

如果用户有保存的密码,例如在 Firefox 中,浏览器将检测到输入有值,然后不会创建占位符文本。

然而,在 Chrome 中,Chrome 直到页面“完成”加载后才会加载保存的值...这意味着它检测到输入为空,创建占位符文本,然后在页面加载后,放置保存的值输入中的值 - 导致占位符和密码相互重叠。

如何检测 Chrome 是否填写了表单以避免这种不幸的重叠?

I'm using jquery to develop placeholder text over an input that fades slightly when the input gets focus, and disappears completely when the users types. This part works fine. So, to avoid a conflict with saved text, I wrapped the code in:

if(!$($input).val()) {

...create placeholder, set up rules for fading it, etc...

}

If the user has a saved password for example, in Firefox, the browser will detect that the input has a value and then not create the placeholder text.

In Chrome, however, Chrome does not load the saved value until after the page is 'done' loading... which means it detects that the input is empty, creates the placeholder text, and then after the page is loaded, places the saved value in the input--leading to the placeholder and the password being on top of each other.

How can I detect Chrome filling out the form to avoid this unfortunate overlap?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

樱花细雨 2024-12-16 11:13:50

最后,我注意到 Chrome 在自动填充时不会触发任何类型的事件,但是如果您等到 window.load 然后检查是否有任何输入具有 -webkit-autofill 您可以检查它们。正如你所看到的,我也在使用去抖器(Ben Alman - http://benalman .com/projects/jquery-throttle-debounce-plugin/)再等待 200 毫秒,因为即使在 window.load 之后,它有时还没有输入自动填充文本......

$(window).load($.debounce(200, function () {

       if ($('input:-webkit-autofill')) {

           // We know that this form has autofill on its way. Let's check this again after a delay as its not always done on window.load

           $('input:-webkit-autofill').each(function () {

               if ($(this).val() !== "") {
                   $(this).parent().find('label').hide();
               }

           });

       }

    }));

In the end, I noticed that Chrome doesnt trigger any sort of event upon autofilling, but if you wait until window.load and then check if any input has -webkit-autofill you can check them then. As you can see I'm also using a debouncer (Ben Alman - http://benalman.com/projects/jquery-throttle-debounce-plugin/) to wait a further 200 milliseconds as even after window.load it sometimes hasn't put in the autofill text yet...

$(window).load($.debounce(200, function () {

       if ($('input:-webkit-autofill')) {

           // We know that this form has autofill on its way. Let's check this again after a delay as its not always done on window.load

           $('input:-webkit-autofill').each(function () {

               if ($(this).val() !== "") {
                   $(this).parent().find('label').hide();
               }

           });

       }

    }));
残花月 2024-12-16 11:13:50

我不知道 Chrome 自动填充何时运行,但在内部

$('input, select').on('focusin', function(evt) {...});

我已经收到所有字段的自动填充值。

(对我来说,这足以测试“脏”表单,即与原始值相比具有更改值的表单。)

I don't know when exactly Chrome autofill run, but inside

$('input, select').on('focusin', function(evt) {...});

I receive autofilled values for all fields already.

(For me this is enough to test for "dirty" forms, ie. forms with changed values compared to original values.)

望笑 2024-12-16 11:13:50

要删除浏览器保存的信息,您可以在表单输入中使用属性 autocomplete="off"

To remove the browser saved information you can use the attribute autocomplete="off" within the form input.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文