表单在使用Reactjs时无法正确提交。

huangapple go评论64阅读模式
英文:

Form not submitting properly using Reactjs

问题

我正在使用Reacjs/nextjs,目前我正在尝试提交"登录表单",我能够弹出警告,但页面也重新加载了,我只想要页面不重新加载,我错在哪里?以下是我的当前代码在 "index.tsx" 文件中:

import React from 'react';
import axios from 'axios';

const LoginForm = () => {
  
  const [formValue, setformValue] = React.useState({
    email: '',
    password: ''
  });

  const handleSubmit = (event) => {
    alert("它正在工作");
    // 我们将在下一段中填充这部分
  }

  const handleChange = (event) => {
    setformValue({
      ...formValue,
      [event.target.name]: event.target.value
    });
  }

  return (
    <form onSubmit={handleSubmit}>
      <p>登录表单</p>
      <input
        type="email"
        name="email"
        placeholder="输入电子邮件"
        value={formValue.email}
        onChange={handleChange}
      />
      <input
        type="password"
        name="password"
        placeholder="输入密码"
        value={formValue.password}
        onChange={handleChange}
      />
      <button
        type="submit"
      >
        登录
      </button>
    </form>
  )
};

export default LoginForm;

希望这有帮助。

英文:

I am working with Reacjs/nextjs,Right now i am trying to submit "login form" i am getting alert but page is also reloading,I just want page should not reload,Where i am wrong ? Here is my current code in "index.tsx"

import React from &#39;react&#39;;
import axios from &#39;axios&#39;;

const LoginForm = () =&gt; {
  
  const [formValue, setformValue] = React.useState({
    email: &#39;&#39;,
    password: &#39;&#39;
  });

  const handleSubmit = (event) =&gt; {
    alert(&quot;its workinggg&quot;);
    // we will fill this in the coming paragraph
  }

  const handleChange = (event) =&gt; {
    setformValue({
      ...formValue,
      [event.target.name]: event.target.value
    });
  }

  return (
    &lt;form onSubmit={handleSubmit}&gt;
      &lt;p&gt;Login Form&lt;/p&gt;
      &lt;input
        type=&quot;email&quot;
        name=&quot;email&quot;
        placeholder=&quot;enter an email&quot;
        value={formValue.email}
        onChange={handleChange}
      /&gt;
      &lt;input
        type=&quot;password&quot;
        name=&quot;password&quot;
        placeholder=&quot;enter a password&quot;
        value={formValue.password}
        onChange={handleChange}
      /&gt;
      &lt;button
        type=&quot;submit&quot;
      &gt;
        Login
      &lt;/button&gt;
    &lt;/form&gt;
  )
};

export default LoginForm;

答案1

得分: 1

使用 preventDefault 方法来阻止默认重新加载。

它适用于所有具有默认操作的事件。

const LoginForm = () => {
  
  const [formValue, setformValue] = React.useState({
    email: '',
    password: ''
  });

  const handleSubmit = (event) => {

    event.preventDefault() // <----- 在这里

    alert("它正在工作");
    // 我们将在接下来的段落中填写这部分内容
  }

  const handleChange = (event) => {
    setformValue({
      ...formValue,
      [event.target.name]: event.target.value
    });
  }

  return (
    <form onSubmit={handleSubmit}>
      <p>登录表单</p>
      <input
        type="email"
        name="email"
        placeholder="输入电子邮件"
        value={formValue.email}
        onChange={handleChange}
      />
      <input
        type="password"
        name="password"
        placeholder="输入密码"
        value={formValue.password}
        onChange={handleChange}
      />
      <button
        type="submit"
      >
        登录
      </button>
    </form>
  )
};
英文:

Use preventDefault method to prevent default reload.

It works with all events which have default action.

const LoginForm = () =&gt; {
  
  const [formValue, setformValue] = React.useState({
    email: &#39;&#39;,
    password: &#39;&#39;
  });

  const handleSubmit = (event) =&gt; {

    event.preventDefault() // &lt;----- HERE

    alert(&quot;its workinggg&quot;);
    // we will fill this in the coming paragraph
  }

  const handleChange = (event) =&gt; {
    setformValue({
      ...formValue,
      [event.target.name]: event.target.value
    });
  }

  return (
    &lt;form onSubmit={handleSubmit}&gt;
      &lt;p&gt;Login Form&lt;/p&gt;
      &lt;input
        type=&quot;email&quot;
        name=&quot;email&quot;
        placeholder=&quot;enter an email&quot;
        value={formValue.email}
        onChange={handleChange}
      /&gt;
      &lt;input
        type=&quot;password&quot;
        name=&quot;password&quot;
        placeholder=&quot;enter a password&quot;
        value={formValue.password}
        onChange={handleChange}
      /&gt;
      &lt;button
        type=&quot;submit&quot;
      &gt;
        Login
      &lt;/button&gt;
    &lt;/form&gt;
  )
};

huangapple
  • 本文由 发表于 2023年2月16日 02:04:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75463805.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定