2

我需要在我的页面中删除所有文本框中的自动完成功能,所以我给出了 <input name="txt_username" id="txt_username" type="text" value="" autocomplete="off" /></dd></dl>

但它不起作用有人知道吗?

4

4 回答 4

10

Autocomplete, unless you're doing something crazy with AJAX, is a client-side thing and you can't always control it like that.

Since autocomplete works by caching your previous entries for a specific input text name, many banks randomly generate the input text name at each form page load but keep track of what is generated either somewhere else in a hidden input element or on the server side.

So instead of

<input name="txt_username" id="txt_username" type="text" value="" autocomplete="off" />

It might be something like

<input type="text" name="f6Lx571p" id="txt_username"/>
<input type="hidden" name="username_key" value="f6Lx571p" />

And the server-side code adjusted accordingly. For example, PHP code might have looked like:

<?php
$user = $_POST['txt_username'];
...

but it would have to be changed to something like:

<?php
$user = $_POST[$_POST['username_key']];
...

Its a bit annoying, but it works.

于 2012-05-02T09:41:57.130 回答
4

无法关闭自动完成功能,它来自浏览器,但我认为这一定有帮助:<input type="password" name="password" readonly onfocus="this.removeAttribute('readonly')">

于 2016-03-23T18:36:36.560 回答
3

You can also try placing that autocomplete attribute on the form element.

<form id="myForm" autocomplete="off">
  ...
</form>

This will probably invalidate your HTML so you might want to consider adding this attribute dynamically with JavaScript.

于 2012-05-02T09:43:45.943 回答
3

Autocomplete cannot be turned off, it's something from the browser. What I do if I want to turn off autocomplete is the following:

Start a session with a field name and random number:

session_start();
$_SESSION['strUsername'] = "username_" . mt_rand(0, 1000000);

Now use this variable as the field's name:

name="' . $_SESSION['strUsername'] . '" id="txt_username" type="text" value="" autocomplete="off" /></dd></dl>

To check the value of the field simply use

$username = $_POST[$_SESSION['strUsername']];

Now, the name will be random everytime, so the browser will not recognize the field and will not give the autocompletion.

于 2012-05-02T09:44:47.200 回答