【发布时间】:2021-02-11 07:59:46
【问题描述】:
Halo 伙计们,我对反应还很陌生,我想提交一个只有带有 enter 按键的文本区域的表单。我遵循了一些 SO 问题,但仍然没有运气,因为它没有被提交。我还想在提交后清除文本区域。如何使用下面的代码做到这一点?
这是我目前拥有的代码。
const { register, handleSubmit, control, errors } = useForm();
const CommentOnSubmit = (data) => {
let formData = new FormData();
formData.append('content', data.content);
formData.append('user', user?.id);
axiosInstance.post(`api/blogs/` + slug + `/comment/`, formData);
} ;
// const commentEnterSubmit = (e) => {
// if(e.key === 'Enter' && e.shiftKey == false) {
// return(
// e.preventDefault(),
// CommentOnSubmit()
// )
// }
// }
<form noValidate onSubmit={handleSubmit(CommentOnSubmit)}>
<div className="post_comment_input">
<textarea
type="text"
placeholder="Write a comment..."
name="content"
ref={register({required: true, maxLength: 1000})}
/>
</div>
<div className="comment_post_button">
<Button type="submit" variant="contained" color="primary">comment</Button>
</div>
</form>
请帮忙。
非常感谢。
【问题讨论】:
标签: javascript reactjs react-native react-redux rxjs