简体   繁体   English

删除输入框自动完成的方法

[英]Ways to remove the autocomplete of an input box

I need a text input field which does not use the autocomplete function - If a user has submitted the form before, his previous submissions should -not- appear as he types into the form again, even if he is typing the same thing again. 我需要一个不使用自动完成功能的文本输入字段 - 如果用户之前已经提交过该表单,那么他以前提交的内容应该会在他再次输入表单时出现,即使他再次键入相同的内容。 As far as I can tell, there are a few ways to do this: 据我所知,有几种方法可以做到这一点:

1. <form autocomplete="off"> 1. <form autocomplete="off">
However, I believe this is a proprietary tag, and I am not sure how compatible it is across browsers 但是,我相信这是一个专有标签,我不确定它与浏览器的兼容性

2. Give the input field a random 'name' 2.为输入字段提供随机的“名称”
One could even use JS to set the name back to an expected value before submission. 甚至可以使用JS在提交之前将名称设置回预期值。 However, if the user does not have JS installed, you'd need another hidden input with the name - and the php code on the other side gets messy fast. 但是,如果用户没有安装JS,则需要另一个带有名称的隐藏输入 - 另一方面的php代码会变得很乱。

Do you know of any other ways? 你知道其他任何方式吗? Is one of these ways the "accepted" way? 这些方式之一是“接受”的方式吗? Comments? 评论?

Thanks, 谢谢,
Mala 马拉

Stick with the random name. 坚持随机名称。 You can do it simply enough server and client and you meet your no-js requirement. 您可以使用足够的服务器和客户端来满足您的no-js要求。

You can store the original and changed name in a $_SESSION variable before outputting the form, and after the user submits, just get the name from there: 您可以在输出表单之前将原始名称和更改的名称存储在$_SESSION变量中,在用户提交之后,只需从中获取名称:

$random_name = md5('original_name' . time());
$_SESSION['original_name'] = $random_name;

...output form...

And after submitting you can easily get the value from $_POST using the $_SESSION variable: 提交后,您可以使用$_SESSION变量轻松地从$_POST获取值:

$field_value = $_POST[$_SESSION['original_name']];

Just be sure that you have sessions available by calling session_start() before any processing. 在进行任何处理之前,请确保通过调用session_start()来确保会话可用。

自动完成是浏览器决定自己做的事情,因此肯定没有要查看的规范文档。

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

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