我正在尝试将各种值从HTML表单保存到sessionStorage但是使用以下代码我收到“storeInfo Start”警报但没有“StoreInfo End”,因此我假设sessionStorage代码存在一些错误/错误。< / p>
我已经删除了其他代码但是这个例子仍然不适合我,我使用的是Firefox 45.0.2,如果这是相关的但是我之前使用了sessionStorage这个浏览器和类似的代码,我找不到区别/ s之间。
function validate () {
alert("Validate");
storeInfo();
}
function storeInfo() {
alert("storeInfo Start");
sessionStorage.firstName = document.getElementById("firstName").value;
alert("StoreInfo End - First Name is: " + sessionStorage.firstName);
}
function init () {
alert("Init Works!");
// For apply page
if (document.getElementById("applyForm") != null) {
var applyForm = document.getElementById("applyForm");
applyForm.onsubmit = validate;
}
}
window.onload = init;
来自HTML文件:
<form id="applyForm">
<p>
<label for="firstName">First Name</label>
<input type="text" name="firstName" id="firstName" pattern="^[a-zA-Z]+$" maxlength="25" size="25" required="required" value="ThisIsMyName" />
</p>
<input type="submit" />
</form>
答案 0 :(得分:0)
你在哪里找到这种API?
使用:
sessionStorage.setItem('firstName', document.getElementById("firstName").value;);
...以及之后sessionStorage.getItem('firstName');
答案 1 :(得分:0)
我不得不在代码片段中添加一些html,但是当我把它放在小提琴上时它会起作用。
<form id="applyForm">
<p>
<label for="firstName">First Name</label>
<input type="text" name="firstName" id="firstName" pattern="^[a-zA-Z]+$" maxlength="25" size="25" required="required" value="ThisIsMyName" />
</p>
<input type="submit" />
</form>
此fiddle有效
答案 2 :(得分:-1)
可能你可以试试localStorage,就像这样:
if(localStorage.getItem('firstName')){
firstName=localStorage.getItem('firstName');
}
localStorage.setItem('firstName',document.getElementById("firstName").value;);