0

大家好!

我有以下三个用于会话存储的功能,我需要一些关于加载部分的帮助。savefunction 工作得很好,但是在加载函数 loadform() 时,我无法将信息返回到表单中。我究竟做错了什么?

function book() {
    document.getElementById('ticketform').addEventListener('input', saveform, false);
    document.getElementById('ticketform').addEventListener('load', loadform, false);
}

function loadform() {
    var storedFnamn = sessionStorage.getItem('text1');
    var storedEnamn = sessionStorage.getItem('text2');
    var storedPnr = sessionStoage.getItem('text3');

    if (storedFnamn) {
        document.getElementById('firstname').value = storedFnamn;
    }
    if (storedEnamn) {
        document.getElementById('lastname').value = storedEnamn;
    }
    if (storedPnr) {
        document.getElementById('idnr').value = storedPnr;
    }
}

function saveform() {
    var fnamn = document.getElementById('firstname').value;
    var enamn = document.getElementById('lastname').value;
    var pnr = document.getElementById('idnr').value;

    window.sessionStorage.setItem('text1', fnamn);
    window.sessionStorage.setItem('text2', enamn);
    window.sessionStorage.setItem('text3', pnr);
}
4

2 回答 2

0

You probably won't get a load event on the form element itself. I think what you want is to replace that line with:

document.addEventListener('DOMContentLoaded', loadform, false);

This way when the page's DOM has finished loading it will call the loadform method and pull in the values from session storage.

于 2016-10-30T12:27:51.477 回答
0

在第 9 行,在storedPnr你拼错的地方sessionStorage,你写了sessionStoage.

于 2016-10-30T12:22:47.197 回答