两个输入字段中的一个只接受一个字母,并自动提交表单。为什么?

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

Among two input fields one is taking just one letter and submit the form automatically. Why?

问题

我正在进行一个MERN博客网站项目的工作。在“写博文”部分有两个输入字段。一个是标题,另一个是描述。

当我尝试写博文时,标题可以完整地输入,但是当我尝试写描述时,只输入一个字母后,表单会自动提交。

如果我先尝试写描述,它也会接受几个单词,但是这次标题只接受一个字母,并在我尝试输入第二个字母时自动提交表单。

我在这里附上了我的代码!
你能帮我解决这个问题吗?

  1. import { useContext, useState } from "react";
  2. import "./write.css";
  3. import { Context } from "../../context/Context";
  4. import axios from "axios";
  5. export default function Write() {
  6. const [title, setTitle] = useState("");
  7. const [desc, setDesc] = useState("");
  8. const [file, setFile] = useState(null);
  9. const { user } = useContext(Context);
  10. const handleSubmit = async (e) => {
  11. e.preventDefault();
  12. const newPost = {
  13. username: user.username,
  14. title,
  15. desc,
  16. };
  17. if (file) {
  18. const data = new FormData();
  19. const filename = Date.now() + file.name;
  20. data.append("name", filename);
  21. data.append("file", file);
  22. newPost.photo = filename;
  23. try {
  24. await axios.post("/upload", data);
  25. } catch (err) {}
  26. }
  27. try {
  28. const res = await axios.post("/posts", newPost);
  29. window.location.replace("/post/" + res.data._id);
  30. } catch (err) {
  31. }
  32. };
  33. return (
  34. <div className="write">
  35. {file &&
  36. <img src={URL.createObjectURL(file)} alt="" className="writeImg" />
  37. }
  38. <form className="writeForm" onChange={handleSubmit}>
  39. <div className="writeFormGroup">
  40. <label htmlFor="fileInput">
  41. <i class="writeIcon fa-solid fa-plus"></i>
  42. </label>
  43. <input type="file" id="fileInput" style={{display:"none"}} onChange={(e)=> setFile(e.target.files[0])} />
  44. <input type="text" placeholder="Title" className="writeInput" autoFocus={true} onChange={e=> setTitle(e.target.value)} />
  45. </div>
  46. <div className="writeFormGroup">
  47. <textarea placeholder="Tell your story..." type="text" className="writeInput writeText" onChange={e=> setDesc(e.target.value)}></textarea>
  48. </div>
  49. <button className="writeSubmit" type="submit">Publish</button>
  50. </form>
  51. </div>
  52. )
  53. }
英文:

I was working on a MERN blog website project. In the Write post section there are two input fields. One is for Title & another is for description.
When I try to write a post the title can be written fully but when I try to write the description the form getting submitted automatically after typing just one letter.

If I try to write the description first it also taking several word but this time the title is taking one letter and submitting the form automatically as sson as I try to type the second letter.

I'm attaching my code here!
Can you please help me to solve this issue?

  1. import { useContext, useState } from &quot;react&quot;
  2. import &quot;./write.css&quot;;
  3. import {Context} from &quot;../../context/Context&quot;;
  4. import axios from &quot;axios&quot;;
  5. export default function Write() {
  6. const [title, setTitle] = useState(&quot;&quot;);
  7. const [desc, setDesc] = useState(&quot;&quot;);
  8. const [file, setFile] = useState(null);
  9. const { user } = useContext(Context);
  10. const handleSubmit = async (e) =&gt; {
  11. e.preventDefault();
  12. const newPost = {
  13. username: user.username,
  14. title,
  15. desc,
  16. };
  17. if (file) {
  18. const data = new FormData();
  19. const filename = Date.now() + file.name;
  20. data.append(&quot;name&quot;, filename);
  21. data.append(&quot;file&quot;, file);
  22. newPost.photo = filename;
  23. try {
  24. await axios.post(&quot;/upload&quot;, data);
  25. } catch (err) {}
  26. }
  27. try {
  28. const res = await axios.post(&quot;/posts&quot;, newPost);
  29. window.location.replace(&quot;/post/&quot; + res.data._id);
  30. } catch (err) {
  31. }
  32. };
  33. return (
  34. &lt;div className=&quot;write&quot;&gt;
  35. {file &amp;&amp;
  36. &lt;img src={URL.createObjectURL(file)} alt=&quot;&quot; className=&quot;writeImg&quot; /&gt;
  37. }
  38. &lt;form className=&quot;writeForm&quot; onChange={handleSubmit}&gt;
  39. &lt;div className=&quot;writeFormGroup&quot;&gt;
  40. &lt;label htmlFor=&quot;fileInput&quot;&gt;
  41. &lt;i class=&quot;writeIcon fa-solid fa-plus&quot;&gt;&lt;/i&gt;
  42. &lt;/label&gt;
  43. &lt;input type=&quot;file&quot; id=&quot;fileInput&quot; style={{display:&quot;none&quot;}} onChange={(e)=&gt; setFile(e.target.files[0])} /&gt;
  44. &lt;input type=&quot;text&quot; placeholder=&quot;Title&quot; className=&quot;writeInput&quot; autoFocus={true} onChange={e=&gt; setTitle(e.target.value)} /&gt;
  45. &lt;/div&gt;
  46. &lt;div className=&quot;writeFormGroup&quot;&gt;
  47. &lt;textarea placeholder=&quot;Tell your story...&quot; type=&quot;text&quot; className=&quot;writeInput writeText&quot; onChange={e=&gt; setDesc(e.target.value)}&gt;&lt;/textarea&gt;
  48. &lt;/div&gt;
  49. &lt;button className=&quot;writeSubmit&quot; type=&quot;submit&quot;&gt;Publish&lt;/button&gt;
  50. &lt;/form&gt;
  51. &lt;/div&gt;
  52. )
  53. }

答案1

得分: 1

在你的表单中,你应该将这个代码改成:<form className="writeForm" onSubmit={handleSubmit}>,而不是<form className="writeForm" onChange={handleSubmit}>
onChange改为onSubmit应该可以解决这个问题。

英文:

In your form, you should change this: &lt;form className=&quot;writeForm&quot; onChange={handleSubmit}&gt; to this: &lt;form className=&quot;writeForm&quot; onSubmit={handleSubmit}&gt;.
Changing onChange to onSubmit should solve the issue

huangapple
  • 本文由 发表于 2023年8月9日 01:49:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76862047.html
匿名

发表评论

匿名网友

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

确定