javascript - 在本地存储的主页上显示数据
问题描述
我正在制作一个将用户保存在本地存储中的网站,我的问题是我需要在主页上显示用户,即用户登录,在表格中显示主页上的用户,我不知道这样做,我尝试了这段代码
<!DOCTYPE html>
<html lang="es-ES">
<head>
<title>Libreria F. Morazan</title>
<meta charset="utf-8" />
</head>
<body>
<div class="container">
<div class="row">
<div class="col-xs-12">
<h1 class="jumbotron">Ejemplo de CRUD usando Local Storage</h1>
<table id="grid" class="table table-striped">
<thead>
<tr>
<th id="name">Name</th>
<th id="email">Correo</th>
<th id="username">Usuario</th>
<th id = "password">contraseña</th>
<th style="width:80px;"></th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>
</div>
<script src="main.js"></script>
<script src="login.js"></script>
</html>
/在主页显示用户的脚本/
<scrip main>
for (i=0; i<localStorage.length; i++) {
var users = [],
dataInLocalStorage = localStorage.getItem(localStorage.key(i)),
gridBody = document.querySelector('#grid tbody');
if (dataInLocalStorage !== null) {
users = JSON.parse(dataInLocalStorage);
}
alert(users);
// Draw TR from TBODY
gridBody.innerHTML = '';
users.forEach(function (x, i) {
var fila = document.createElement("tr"),
tdName = document.createElement("td"),
tdJob = document.createElement("td"),
tdAge = document.createElement("td"),
tdps = document.createElement("td"),
tdRemove = document.createElement("td"),
btnRemove = document.createElement("button");
tdName.innerHTML = x.name;
tdJob.innerHTML = x.email;
tdAge.innerHTML = x.username;
tdps.innerHTML = x.password
btnRemove.textContent = 'Remove';
btnRemove.className = 'btn btn-xs btn-danger';
btnRemove.addEventListener('click', function(){
removeFromLocalStorage(i);
});
tdRemove.appendChild(btnRemove);
fila.appendChild(tdName);
fila.appendChild(tdJob);
fila.appendChild(tdAge);
fila.appendChild(tdRemove);
gridBody.appendChild(fila);
});
}
/ js将用户保存在localstorage中/
<scrip libreria>
window.onload = function () {
var localStorageKeyName;
document
.querySelector("#create-account")
.addEventListener("click", function () {
localStorageKeyName = document.getElementById("email").value;
var name = document.getElementById("name"),
correo = document.getElementById("email"),
usuario = document.getElementById("username"),
contraseña = document.getElementById("password");
confContrasenia = document.getElementById("password-again");
// Validate
if (
name.value.length === 0 ||
correo.value.length === 0 ||
usuario.value.length === 0 ||
contraseña.value.length === 0
) {
alert("Ingrese todos los campos");
} else {
if (contraseña.value == confContrasenia.value) {
var user = {
name: name.value,
correo: correo.value,
usuario: usuario.value,
contraseña: contraseña.value,
};
// Clean data
name.value = "";
correo.value = "";
usuario.value = "";
contraseña.value = "";
// Append to my localStorage
appendObjectToLocalStorage(user);
window.location.replace("login.html");
} else {
alert("Las contraseñas no son iguales, ingreselas una vez mas!");
location.href("index.html");
}
}
});
function appendObjectToLocalStorage(obj) {
var users = [],
dataInLocalStorage = localStorage.getItem(localStorageKeyName);
if (dataInLocalStorage !== null) {
users = JSON.parse(dataInLocalStorage);
}
users.push(obj);
localStorage.setItem(localStorageKeyName, JSON.stringify(users));
}
};
解决方案
推荐阅读
- blueprism - 在第 6 版中,试图收集法语字母但得到特殊字符
- javascript - 边缘说 Babel 代码预期的功能
- c++ - 在 Doxygen 中使用 Microsoft 的源代码注释语言 (SAL)?
- css - 选择/组合弹出溢出溢出滚动元素
- r - 使用 data.table 对多列执行操作
- javascript - MSE SouceBuffer 对象不支持“audio/wav”mime 类型,但指向 .wav 的音频 src 属性可以正常播放
- python - 为什么这个 for 循环会抛出 TypeError?
- java - 升级到 'com.google.android.gms:play-services-ads:18.1.0' 后的 Android 编译错误
- algorithm - 李超树 vs 凸壳技巧。应该首选哪个以及何时?
- swift - 以编程方式锚定视图组件会产生错误