从下面的代码中提交后,如何删除表单值?
提交表格后,如何删除输入数据?
import React from 'react';
import { Form } from 'react-bootstrap';
const AddItem = () => {
const handleItemSubmit = (event) => {
event.preventDefault();
const carName = event.target.carName.value;
const companyName = event.target.companyName.value;
console.log(carName, companyName);
}
return (
<div className='w-50 mx-auto mt-5 py-5 d-block'>
<Form onSubmit={handleItemSubmit}>
<Form.Group className="mb-3" controlId="formBasicCarName">
<Form.Control name="carName" type="text" placeholder="Enter Car Model Name" />
</Form.Group>
<Form.Group className="mb-3" controlId="formBasicCompany">
<Form.Control name="companyName" type="text" placeholder="Enter Company Name" />
</Form.Group>
<button className='btn btn-primary' variant="primary" type="submit">
Submit
</button>
</Form>
</div>
);
};
export default AddItem;
在这里,我获取了两个输入,并使用OnSubmit获取数据。蚂蚁我可以轻松获取数据。但是,我想在以“提交”为“提交”的同一按钮提交后重置该值。
How can I remove the Input data after submitting the form?
import React from 'react';
import { Form } from 'react-bootstrap';
const AddItem = () => {
const handleItemSubmit = (event) => {
event.preventDefault();
const carName = event.target.carName.value;
const companyName = event.target.companyName.value;
console.log(carName, companyName);
}
return (
<div className='w-50 mx-auto mt-5 py-5 d-block'>
<Form onSubmit={handleItemSubmit}>
<Form.Group className="mb-3" controlId="formBasicCarName">
<Form.Control name="carName" type="text" placeholder="Enter Car Model Name" />
</Form.Group>
<Form.Group className="mb-3" controlId="formBasicCompany">
<Form.Control name="companyName" type="text" placeholder="Enter Company Name" />
</Form.Group>
<button className='btn btn-primary' variant="primary" type="submit">
Submit
</button>
</Form>
</div>
);
};
export default AddItem;
Here I Took two input and get the data by using OnSubmit. Ant I can get the data easily. But I want to reset the value after submit with same button called "submit".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
因此,为了删除重置形式,您应该使用受控表单。
通过受控形式,我的意思是使用状态更改表单输入。这是推荐的方法和最佳实践。
因此,如果您必须重写您的代码,它将看起来像这样。
So, In order to remove the reset the form you should use the controlled forms.
By controlled forms i mean using the states to change form inputs. And that's the recommended way and best practice.
so if you'll have to re-write your your code it'll look something like this.
只需在功能末尾添加
Just add at the end of function
重置这样的表格:
reset the form like this:
简单的修复是重置这样的表格。
但是,不要忘记将ID提供给您的表格
,我强烈建议您在上面查看有关USESTATE的CallMeizaz答案。那是处理形式的正确方法
Easy fix is to reset the form like this.
And don't forget to Provide the id to your form
However, i will highly suggest you to look into callmeizaz answer above about the useState. Thats the proper way to handle form