css 本地存储和输入字段的错误[已关闭]

r1zk6ea1  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(48)

**已关闭。**此问题需要debugging details。目前不接受回答。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答问题。
8天前关闭。
Improve this question
我创建了一个本地存储的代码,到目前为止一切都很好,当页面重新加载时,其他的东西都保存了,但是输入字段中的文本消失了。我应该怎么办?
我尝试将本地存储数据添加到输入字段并创建,我希望它在页面重新加载时保存文本,但它没有保存它。输入在span元素中,但使用JavaScript创建

fsi0uk1n

fsi0uk1n1#

此示例完全有效:

<html>
    <head>
        <script>
        function change() {
            localStorage.setItem("myinput", this.value);
        }

        window.addEventListener("load", function() {
            let storedValue = localStorage.getItem("myinput");
            if (storedValue) {
                document.getElementById("foo").value = storedValue;
            }
        });
        </script>
    </head>
    <body>
        <input type="text" oninput="change" id="foo">
    </body>
</html>

字符串
正如您所见,我们可以让它正常工作。因此,在您尝试让它正常工作的过程中出现了一些问题。由于input是通过JavaScript在您的终端添加的,您可能会在创建输入之前尝试添加事件处理程序。JavaScript.

相关问题