简体   繁体   English

具有自动完成功能但没有默认值的HTML输入

[英]HTML input with autocomplete but no default value

I would like to have an input element inside a form with the usual browser autocomplete but i don't want the browser to set a default value to it. 我想在表单中使用通常的浏览器自动完成功能的输入元素,但我不希望浏览器为其设置默认值。

The expected behavior would be that the input is always rendered empty and when the user clicks it the browser autocompletes kicks in displaying the previous entered values for that field. 预期的行为是输入始终为空,并且当用户单击该输入时,浏览器会自动完成踢,以显示该字段的先前输入值。

I've seen something similar in this login page. 我在登录页面中看到了类似的内容。

I've tried emptying the input values using javascript on page load but it's not a very clean solution as sometimes i can see the default values showing up and then getting erased. 我尝试在页面加载时使用javascript清空输入值,但这不是一个很干净的解决方案,因为有时我会看到默认值出现然后被擦除。

Any help would be great. 任何帮助都会很棒。

I think this would be your answer... http://liveweave.com/dnEfCD 我认为这将是您的答案... http://liveweave.com/dnEfCD

Just use the 'placeholder' attribute. 只需使用“占位符”属性即可。

 <form id="main">
<input id="email" type="text" placeholder="Email" name="email">
<input id="password" type="password" placeholder="Password" name="password">
 </form>

wantworthy.com/login creates the page with JavaScript. wantworthy.com/login使用JavaScript创建页面。 So when it loads, the input fields are not there and the browser can't fill them. 因此,加载时,输入字段不存在,浏览器无法填充它们。

If you don't want to go so far you could only create the input fields after the page has loaded. 如果您不想走那么远,则只能在页面加载后创建输入字段。 Maybe something even simpler like changing the names of the fields could to the trick (Fake names before, real names after the page has loaded). 也许更简单的操作(如更改字段名称)可以解决问题(加载之前为假名,加载页面后为实名)。 But you have to figure out when the browser fills the fields: onDocumentReady, onLoad or even later. 但是您必须确定浏览器何时填写以下字段:onDocumentReady,onLoad或什至更高版本。

As written in the comments you can use autocomplete="off" in the form tag if you want no browser intervention at all. 如注释中所述,如果您根本不需要浏览器干预,则可以在form标记中使用autocomplete="off" But that would turn off displaying previously entered values as well. 但这也将关闭显示以前输入的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM