简体   繁体   English

如何从 sessionStorage 将表单数据临时存储在用户的计算机上?

[英]How do I temporarily store form data on a user's computer from sessionstorage?

I created a registration form in my solid-js application.我在我的solid-js 应用程序中创建了一个注册表单。 I would like that after the submission of the form, that the values entered disappear from the input fields of the form but are kept on the browser of the user temporarily (until the shutdown of the computer) in order to be able to get these values later in other code files.我希望在提交表单后,输入的值会从表单的输入字段中消失,但会暂时保留在用户的浏览器上(直到计算机关闭),以便能够获取这些值稍后在其他代码文件中。 For this, I therefore decided to use sessionstorage and wrote the following code:为此,我因此决定使用 sessionStorage 并编写以下代码:

const [form, setForm] = createStore({
    firstName: "",
    lastName: "",
    email: "",
    password: "",
    gender: "",
  });

    sessionStorage.setItem('firstName',form.firstName)
    sessionStorage.setItem('lastName',form.lastName)
    sessionStorage.setItem('email',form.email)
    sessionStorage.setItem('password',form.password)
    sessionStorage.setItem('gender',form.gender)

And in order to use these values in other files, I do this:为了在其他文件中使用这些值,我这样做:

    const firstName = sessionStorage.getItem('firstName')
    const lastName = sessionStorage.getItem('lastName')
    const email = sessionStorage.getItem('email')
    const password = sessionStorage.getItem('password')
    const gender = sessionStorage.getItem('gender')

because I think that since the values are kept on the user's computer, it is possible to directly access the value of these variables.因为我认为由于这些值保存在用户的计算机上,因此可以直接访问这些变量的值。 But the values of the specified keys are not returned by sessionstorage.但是指定键的值不会被 sessionStorage 返回。 I looked on this stackoverflow question and also on this one but nothing.我查看了这个stackoverflow问题,也查看了这个问题,但什么也没有。 Thanks!谢谢!

You don't have the glue code between the form field and the local storage.您没有表单域和本地存储之间的粘合代码。 Here is a simple example using a signal but you can update it to use a store:这是一个使用信号的简单示例,但您可以将其更新为使用商店:

import { render } from "solid-js/web";
import { createSignal, onMount } from "solid-js";

function App() {
  let ref: HTMLInputElement | undefined;

  const [val, setVal] = createSignal();

  const handleInput = (event: any) => {
    if (event) {
      sessionStorage.setItem('some-value', event.target.value);
    }
  };

  onMount(() => {
    const someValue = sessionStorage.getItem("some-value");
    setVal(someValue);
  });

  return (
    <div>
      <input value={val()} type="text" ref={ref} onInput={handleInput} />
    </div>
  );
}

render(() => <App />, document.getElementById("app")!);

We restore the stored value using onMount hook which runs after the element is rendered.我们使用onMount钩子恢复存储的值,该钩子在元素渲染后运行。

We store the new value whenever the input field is updated using onInput handler.每当使用onInput处理程序更新输入字段时,我们都会存储新值。 Unlike React, Solid keeps close to HTML standards and does not use onChange .与 React 不同,Solid 保持接近 HTML 标准并且不使用onChange

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM