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

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

Form not submitting properly using Reactjs

问题

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

  1. import React from 'react';
  2. import axios from 'axios';
  3. const LoginForm = () => {
  4. const [formValue, setformValue] = React.useState({
  5. email: '',
  6. password: ''
  7. });
  8. const handleSubmit = (event) => {
  9. alert("它正在工作");
  10. // 我们将在下一段中填充这部分
  11. }
  12. const handleChange = (event) => {
  13. setformValue({
  14. ...formValue,
  15. [event.target.name]: event.target.value
  16. });
  17. }
  18. return (
  19. <form onSubmit={handleSubmit}>
  20. <p>登录表单</p>
  21. <input
  22. type="email"
  23. name="email"
  24. placeholder="输入电子邮件"
  25. value={formValue.email}
  26. onChange={handleChange}
  27. />
  28. <input
  29. type="password"
  30. name="password"
  31. placeholder="输入密码"
  32. value={formValue.password}
  33. onChange={handleChange}
  34. />
  35. <button
  36. type="submit"
  37. >
  38. 登录
  39. </button>
  40. </form>
  41. )
  42. };
  43. 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"

  1. import React from &#39;react&#39;;
  2. import axios from &#39;axios&#39;;
  3. const LoginForm = () =&gt; {
  4. const [formValue, setformValue] = React.useState({
  5. email: &#39;&#39;,
  6. password: &#39;&#39;
  7. });
  8. const handleSubmit = (event) =&gt; {
  9. alert(&quot;its workinggg&quot;);
  10. // we will fill this in the coming paragraph
  11. }
  12. const handleChange = (event) =&gt; {
  13. setformValue({
  14. ...formValue,
  15. [event.target.name]: event.target.value
  16. });
  17. }
  18. return (
  19. &lt;form onSubmit={handleSubmit}&gt;
  20. &lt;p&gt;Login Form&lt;/p&gt;
  21. &lt;input
  22. type=&quot;email&quot;
  23. name=&quot;email&quot;
  24. placeholder=&quot;enter an email&quot;
  25. value={formValue.email}
  26. onChange={handleChange}
  27. /&gt;
  28. &lt;input
  29. type=&quot;password&quot;
  30. name=&quot;password&quot;
  31. placeholder=&quot;enter a password&quot;
  32. value={formValue.password}
  33. onChange={handleChange}
  34. /&gt;
  35. &lt;button
  36. type=&quot;submit&quot;
  37. &gt;
  38. Login
  39. &lt;/button&gt;
  40. &lt;/form&gt;
  41. )
  42. };
  43. export default LoginForm;

答案1

得分: 1

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

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

  1. const LoginForm = () => {
  2. const [formValue, setformValue] = React.useState({
  3. email: '',
  4. password: ''
  5. });
  6. const handleSubmit = (event) => {
  7. event.preventDefault() // <----- 在这里
  8. alert("它正在工作");
  9. // 我们将在接下来的段落中填写这部分内容
  10. }
  11. const handleChange = (event) => {
  12. setformValue({
  13. ...formValue,
  14. [event.target.name]: event.target.value
  15. });
  16. }
  17. return (
  18. <form onSubmit={handleSubmit}>
  19. <p>登录表单</p>
  20. <input
  21. type="email"
  22. name="email"
  23. placeholder="输入电子邮件"
  24. value={formValue.email}
  25. onChange={handleChange}
  26. />
  27. <input
  28. type="password"
  29. name="password"
  30. placeholder="输入密码"
  31. value={formValue.password}
  32. onChange={handleChange}
  33. />
  34. <button
  35. type="submit"
  36. >
  37. 登录
  38. </button>
  39. </form>
  40. )
  41. };
英文:

Use preventDefault method to prevent default reload.

It works with all events which have default action.

  1. const LoginForm = () =&gt; {
  2. const [formValue, setformValue] = React.useState({
  3. email: &#39;&#39;,
  4. password: &#39;&#39;
  5. });
  6. const handleSubmit = (event) =&gt; {
  7. event.preventDefault() // &lt;----- HERE
  8. alert(&quot;its workinggg&quot;);
  9. // we will fill this in the coming paragraph
  10. }
  11. const handleChange = (event) =&gt; {
  12. setformValue({
  13. ...formValue,
  14. [event.target.name]: event.target.value
  15. });
  16. }
  17. return (
  18. &lt;form onSubmit={handleSubmit}&gt;
  19. &lt;p&gt;Login Form&lt;/p&gt;
  20. &lt;input
  21. type=&quot;email&quot;
  22. name=&quot;email&quot;
  23. placeholder=&quot;enter an email&quot;
  24. value={formValue.email}
  25. onChange={handleChange}
  26. /&gt;
  27. &lt;input
  28. type=&quot;password&quot;
  29. name=&quot;password&quot;
  30. placeholder=&quot;enter a password&quot;
  31. value={formValue.password}
  32. onChange={handleChange}
  33. /&gt;
  34. &lt;button
  35. type=&quot;submit&quot;
  36. &gt;
  37. Login
  38. &lt;/button&gt;
  39. &lt;/form&gt;
  40. )
  41. };

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:

确定