我需要存储用户通过 html5 在每个框中输入的值。当用户刷新页面时,它应该保留每个文本框中的值。我有一个朋友告诉我这会起作用,但事实并非如此,而且我在控制台中没有收到任何错误。
<script>
function storageValue() {
var uno = document.getElementById("storage1");
var duck = uno.value
var dos = document.getElementById("storage2");
var duckie = dos.value
var tres = document.getElementById("storage3");
var rubberDuck = tres.value
var quatro = document.getElementById("storage4");
var rubberDuckie = quatro.value
alert("Your stored values are: " + duck + "," + duckie + "," + rubberDuck + "," + rubberDuckie);
localStorage.setItem('duck', duck);
localStorage.setItem('duckie', duckie);
localStorage.setItem('rubberDuck', rubberDuck);
localStorage.setItem('rubberDuckie', rubberDuckie);
checkLocalStorage();
}
function checkLocalStorage() {
var poodle = document.getElementById('storage1').value
poodle.innerHTML = localStorage["duck"]
var cow = document.getElementById('storage2').value
poodle.innerHTML = localStorage["duckie"]
var dog = document.getElementById('storage3').value
dog.innerHTML = localStorage["rubberDuck"]
var cat = document.getElementById('storage4').value
cat.innerHTML = localStorage["rubberDuckie"];
}
checkLocalStorage();
这是我拥有的 HTML:
<body align="center" style="background-color:red;">
<div>
<header>
<h1>Local Storage</h1>
</header>
<input type="text" id="storage1" size="40" placeholder="Please enter a value">
<input type="text" id="storage2" size="40" placeholder="Please enter a value">
<input type="text" id="storage3" size="40" placeholder="Please enter a value">
<input type="text" id="storage4" size="40" placeholder="Please enter a value">
<br>
<br>
<input type="button" id="addValue" value="Store Input Values" onclick='storageValue();'>
<div id="storageDiv"></div>
<nav>
<p>
<a href="/">Home</a>
</p>
<p>
<a href="/contact">Contact</a>
</p>
</nav>
<div>
</div>
<footer>
<p>
© Copyright by Alan Sylvestre
</p>
</footer>
</div>
</body>