javascript - 试图从表单中检索 cookie 并仅在 javascript 中显示他的信息
问题描述
我实际上是法国人,我为即将犯下的错误感到抱歉。对于我的项目,我需要检索我的表单信息(姓名、姓氏、电子邮件)并在带有 cookie 的另一个页面上显示信息。我对javascript不太了解,也不知道该怎么做。
<form action="#" method="POST">
Nom : <input type="text" name="data">
Prénom : <input type="text" name="data">
Email : <input type="text" name="data">
<input type="submit" value="Validez">
</form>
<script>
function creerCookie(nom, valeur, jour){
if(jour) {
var date = new Date();
date.setTime(date.getTime()+(jour*24*60*60*1000));
var exp = '; expires='+date.toGMTString();
}
else{
var exp = '';
}
document.cookie = nom+'='+valeur+exp+';path=/';
}
function lireCookie(nom){
var nomEtEgal = nom + '=';
var cTableau = document.cookie.split(';');
for(var i= 0 ; i < cTableau.length; i++){
var c = cTableau[i];
while(c.charAt(0)==' '){
c = c.substring(1, c.length);
}
if(c.indexOf(nomEtEgal) == 0){
return c.substring(nomEtEgal.length, c.length);
}
}
return null
}
function supprimerCookie(nom){
creerCookie(nom,'', -1);
}
creerCookie('Cookie1',"Je suis un cookie", 7);
var affiche = document.getElementById('para');
affiche.innerHTML = 'Valeur :' + lireCookie('Cookie1');
</script>
解决方案
使用 localStorage 更容易,这里有一个简短的演示:https ://codepen.io/timotgl/pen/jOrdoMp
<form>
<p>
First name: <input type="text" name="firstName" />
</p>
<p>
Last name: <input type="text" name="lastName" />
</p>
<p>
Email : <input type="text" name="email" />
</p>
</form>
<button id="save">Save</button>
<script>
// Read input values from localStorage
let firstName = localStorage.getItem('firstName') || '';
let lastName = localStorage.getItem('lastName') || '';
let email = localStorage.getItem('email') || '';
// Find input elements
const firstNameInput = document.querySelector('input[name="firstName"]');
const lastNameInput = document.querySelector('input[name="lastName"]');
const emailInput = document.querySelector('input[name="email"]');
// Restore input values
firstNameInput.value = firstName;
lastNameInput.value = lastName;
emailInput.value = email;
// Save what the user types
firstNameInput.addEventListener('change', (event) => {
firstName = event.target.value;
});
lastNameInput.addEventListener('change', (event) => {
lastName = event.target.value;
});
emailInput.addEventListener('change', (event) => {
email = event.target.value;
});
// Write everything to localStorage if save button is clicked
const saveButton = document.querySelector('button#save');
saveButton.addEventListener('click', () => {
localStorage.setItem('firstName', firstName);
localStorage.setItem('lastName', lastName);
localStorage.setItem('email', email);
});
</script>
推荐阅读
- .htaccess - 将 http 重定向到 https 在子目录中作为 404 出现问题 - 未找到
- apache-spark - 作业和任务持续时间的关系是 Spark UI
- javascript - 仅使用正则表达式保留字母数字值
- protocols - Session Initiation Protocol 如何表示有效载荷数据?
- karate - 如何在空手道中切换到窗口(不是 TAB)?
- php - PHP致命错误:未捕获的错误:调用未定义的函数sqlite_open()
- node.js - 变量 x 保存以下字符串数据。我如何解析它以用作对象
- groovy - 如何解释这个 Groovy 表达式?
- python - 如何让python通过cmake在自定义位置查找numpy(用于构建C++共享对象库)
- single-sign-on - 在 openiddict 中使用 iframe 单点注销,如何知道已登录的客户端应用程序?