如何判断浏览器是否已自动填充文本框?特别是用户名和密码框,自动填充页面加载。
我的第一个问题是,这在页面加载序列中什么时候发生?是在document.ready之前还是之后?
其次,我如何使用逻辑来找出是否发生了这种情况?这不是我想阻止这种情况发生,只是挂钩到事件。最好是这样的:
if (autoFilled == true) {
} else {
}
如果可能的话,我很想看到一个jsfiddle显示你的答案。
可能重复
DOM事件浏览器密码自动填充?
浏览器自动填充和Javascript触发事件
这两个问题都没有真正解释什么事件被触发,它们只是不断地重新检查文本框(对性能不好!)
我遇到过同样的问题,我已经写出了这个解。
当页面加载时,它开始对每个输入字段进行轮询(我设置了10秒,但您可以调优这个值)。
10秒后,它将停止对每个输入字段的轮询,只对集中的输入(如果有的话)开始轮询。
当你模糊输入时,它会停止,如果你聚焦一个,它又会开始。
通过这种方式,您只在真正需要时轮询,并且只对有效输入进行轮询。
// This part of code will detect autofill when the page is loading (username and password inputs for example)
var loading = setInterval(function() {
$("input").each(function() {
if ($(this).val() !== $(this).attr("value")) {
$(this).trigger("change");
}
});
}, 100);
// After 10 seconds we are quite sure all the needed inputs are autofilled then we can stop checking them
setTimeout(function() {
clearInterval(loading);
}, 10000);
// Now we just listen on the focused inputs (because user can select from the autofill dropdown only when the input has focus)
var focused;
$(document)
.on("focus", "input", function() {
var $this = $(this);
focused = setInterval(function() {
if ($this.val() !== $this.attr("value")) {
$this.trigger("change");
}
}, 100);
})
.on("blur", "input", function() {
clearInterval(focused);
});
当自动插入多个值时,它的工作效果不太好,但可以对它进行调整,查找当前表单上的每个输入。
喜欢的东西:
// This part of code will detect autofill when the page is loading (username and password inputs for example)
var loading = setInterval(function() {
$("input").each(function() {
if ($(this).val() !== $(this).attr("value")) {
$(this).trigger("change");
}
});
}, 100);
// After 10 seconds we are quite sure all the needed inputs are autofilled then we can stop checking them
setTimeout(function() {
clearInterval(loading);
}, 10000);
// Now we just listen on inputs of the focused form
var focused;
$(document)
.on("focus", "input", function() {
var $inputs = $(this).parents("form").find("input");
focused = setInterval(function() {
$inputs.each(function() {
if ($(this).val() !== $(this).attr("value")) {
$(this).trigger("change");
}
});
}, 100);
})
.on("blur", "input", function() {
clearInterval(focused);
});
似乎有一个解决方案,不依赖轮询(至少Chrome)。它几乎是一样的粗鄙,但我确实认为比全球民意调查好一点。
考虑以下场景:
用户开始填写字段1
用户选择一个自动完成建议,自动填充field2和field3
解决方案:在所有字段上注册一个onblur,通过下面的jQuery片段$(':-webkit-autofill')检查自动填充字段的存在
这不会是立即的,因为它会延迟到用户模糊field1,但它不依赖于全局轮询,所以在我看来,这是一个更好的解决方案。
也就是说,由于按回车键可以提交表单,您可能还需要相应的onkeypress处理程序。
或者,你可以使用全局轮询来检查$(':-webkit-autofill')