用对象设置FormData

发布于 2025-02-08 19:13:18 字数 1991 浏览 0 评论 0原文

目前,我的状态具有一个formdata。

键入一些文本后,以更改fullName.firstName。它制造另一个属性,只是设置一个(如单个字母)值。

const [formData, setFormData] = useState({
    fullName: {
      firstName: "",
      lastName: "",
    },
    email: "",
    password: "",
    confirmPassword: "",
  });

这就是我设置FormData的方式。

 const handleChange = (event) => {
    const { value, name } = event.target;
    console.log(name, value);

    setFormData((prevFormData) => ({
      ...prevFormData,
      [name]: value,
    }));
  };

这是我的JSX,您可以在输入中检查输入中的“名称”属性以获取一些参考。

<div className="App">
      <form onSubmit={onSubmit}>
        <h1>Submit Form Nested Object UseState</h1>

        <input
          text="text"
          placeholder="First Name"
          name="firstName"
          value={formData.fullName.firstName}
          onChange={handleChange}
        />
        <input
          text="text"
          placeholder="Last Name"
          name="lastName"
          value={formData.fullName.lastName}
          onChange={handleChange}
        />
        <input
          text="email"
          placeholder="email"
          name="email"
          value={formData.email}
          onChange={handleChange}
        />
        <input
          text="password"
          placeholder="password"
          name="password"
          value={formData.password}
          onChange={handleChange}
        />
        <input
          text="password"
          placeholder="confirm Password"
          name="confirmPassword"
          value={formData.confirmPassword}
          onChange={handleChange}
        />
        <button>Submit</button>
      </form>
      {JSON.stringify(formData, null, 2)}
    </div>

Currently, i have this state with a formData.

Upon typing some text, instead to change the fullName.firstName. its making another property and just setting a single (as in single letter) value.

enter image description here

const [formData, setFormData] = useState({
    fullName: {
      firstName: "",
      lastName: "",
    },
    email: "",
    password: "",
    confirmPassword: "",
  });

This is how i set the formData.

 const handleChange = (event) => {
    const { value, name } = event.target;
    console.log(name, value);

    setFormData((prevFormData) => ({
      ...prevFormData,
      [name]: value,
    }));
  };

This is my JSX, you may check the "name" attribute in input for some reference.

<div className="App">
      <form onSubmit={onSubmit}>
        <h1>Submit Form Nested Object UseState</h1>

        <input
          text="text"
          placeholder="First Name"
          name="firstName"
          value={formData.fullName.firstName}
          onChange={handleChange}
        />
        <input
          text="text"
          placeholder="Last Name"
          name="lastName"
          value={formData.fullName.lastName}
          onChange={handleChange}
        />
        <input
          text="email"
          placeholder="email"
          name="email"
          value={formData.email}
          onChange={handleChange}
        />
        <input
          text="password"
          placeholder="password"
          name="password"
          value={formData.password}
          onChange={handleChange}
        />
        <input
          text="password"
          placeholder="confirm Password"
          name="confirmPassword"
          value={formData.confirmPassword}
          onChange={handleChange}
        />
        <button>Submit</button>
      </form>
      {JSON.stringify(formData, null, 2)}
    </div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聽兲甴掵 2025-02-15 19:13:18

更改表单名称:

      <form onSubmit={onSubmit}>
        <h1>Submit Form Nested Object UseState</h1>

        <input
          placeholder="First Name"
          name="fullName.firstName"
          value={formData.fullName.firstName}
          onChange={handleChange}
        />
        <input

          placeholder="Last Name"
          name="fullName.lastName"
          value={formData.fullName.lastName}
          onChange={handleChange}
        />
        <input

          placeholder="email"
          name="email"
          value={formData.email}
          onChange={handleChange}
        />
        <input

          placeholder="password"
          name="password"
          value={formData.password}
          onChange={handleChange}
        />
        <input

          placeholder="confirm Password"
          name="confirmPassword"
          value={formData.confirmPassword}
          onChange={handleChange}
        />
        <button>Submit</button>
      </form>

然后将您的处理功能更改为:

  const handleChange = (event) => {
    const { value } = event.target;
    const [key,subkey] = event.target.name.split('.');

    setFormData((prevFormData) => ({
      ...prevFormData,
      [key]: subkey ? {
        ...prevFormData[key],
        [subkey]: value,
      } : value
    }));
  };

Change the form names like so:

      <form onSubmit={onSubmit}>
        <h1>Submit Form Nested Object UseState</h1>

        <input
          placeholder="First Name"
          name="fullName.firstName"
          value={formData.fullName.firstName}
          onChange={handleChange}
        />
        <input

          placeholder="Last Name"
          name="fullName.lastName"
          value={formData.fullName.lastName}
          onChange={handleChange}
        />
        <input

          placeholder="email"
          name="email"
          value={formData.email}
          onChange={handleChange}
        />
        <input

          placeholder="password"
          name="password"
          value={formData.password}
          onChange={handleChange}
        />
        <input

          placeholder="confirm Password"
          name="confirmPassword"
          value={formData.confirmPassword}
          onChange={handleChange}
        />
        <button>Submit</button>
      </form>

then change your handleChange function to this:

  const handleChange = (event) => {
    const { value } = event.target;
    const [key,subkey] = event.target.name.split('.');

    setFormData((prevFormData) => ({
      ...prevFormData,
      [key]: subkey ? {
        ...prevFormData[key],
        [subkey]: value,
      } : value
    }));
  };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文