自动填充功能不会自动完成

时间:2015-06-05 08:25:39

标签: javascript autocomplete local-storage html-form

Here is a page

<form action="action.php" method="get" autocomplete="on">
  First name:<input type="text" name="fname"><br>
  E-mail: <input type="email" name="email"><br>
  <input type="submit">
</form> 

让我们输入2个字段,然后提交。然后在重新加载页面时,字段不会自动填充,为什么?

我应该使用本地存储吗?饼干?确保几天后回来的用户仍然会自动填充其字段吗?

(我使用的Firefox有关于自动完成的默认选项,即未禁用)。

1 个答案:

答案 0 :(得分:3)

首先,您需要意识到元素上没有任何编码的属性完全是浏览器实现的。

浏览器以不同的方式实现这些功能。

您应该检查the spec。你会注意到,实际上,

  

默认情况下,表单元素的自动完成属性处于开启状态。

解决问题的唯一方法就是说,如果您想要更可靠和一致的用户体验;不要依赖浏览器实现。

简答:使用Javascript

有数百个插件可以帮到你。我会指出你最近在github上流行的一个但是需要你真的需要告诉这个插件你期望它做什么:https://leaverou.github.io/awesomplete/

更新答案:

我看到你只是想预备这个领域。这可以通过类似的方式完成(未经测试):

defined? ur_variable
=> nil

ur_variable= 12
defined? ur_variable
=> "local-variable"

another_variable = nil
defined? another_variable
=> "local-variable"