简体   繁体   English

如何预填写用户输入表格?

[英]How to prefill the user input form?

I have a very basic form where I ask user's name, mobile, email. 我有一个非常基本的表格,要求输入用户名,手机号和电子邮件。 I have enabled autocomplete provision in these fields using the following code: 我已使用以下代码在这些字段中启用了自动完成设置:

<label for="frmNameA">Name</label>
<input name="name" id="frmNameA" placeholder="Full name" required autocomplete="name">

<label for="frmEmailA">Email</label>
<input type="email" name="email" id="frmEmailA" placeholder="name@example.com" required autocomplete="email">


<label for="frmPhoneNumA">Phone</label>
<input type="tel" name="phone" id="frmPhoneNumA" placeholder="+1-650-450-1212" required autocomplete="tel">

Here is the fiddle. 是小提琴。 Currently, the user has to focus on these elements and then it displays the suggestions. 当前,用户必须专注于这些元素,然后显示建议。 Is there any way by which we can prefill this data once the form is loaded? 表单加载后,是否可以通过任何方式预填充此数据? We want to prefill the 1st suggestion that comes in the autocomplete and not some hard coded value. 我们要预填充自动填充中的第一个建议,而不是一些硬编码值。

Also, I saw websites like facebook, twitter etc. using only autocomplete and not prefilling the data - is there any specific reason to not prefill the things? 另外,我看到诸如facebook,twitter等的网站仅使用自动完成功能而不预填充数据-是否有特定原因不预填充数据?

The autocomplete attribute specifies whether a form should have autocomplete on or off. autocomplete属性指定表单应启用还是禁用自动完成功能。

When autocomplete is on, the browser automatically complete values based on values that the user has entered before. 启用自动完成功能后,浏览器会根据用户之前输入的值自动完成值。

<form action="/action_page.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>

You can store user values into the localStorage at completion, then load this values on page load. 您可以在完成时将用户值存储到localStorage中,然后在页面加载时加载此值。

This way, no need to hardcode datas, neither store server side. 这样,无需对数据进行硬编码,也无需存储服务器端。

// Store 
localStorage.setItem("userconfig",mail.value)

// Fill data
onload = (function(){
  mail.value =   localStorage.getItem("userconfig")

})

在此处输入图片说明

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

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