[英]How do you submit a form after hiding the submit button in React
I'm trying to submit a form with a form action.我正在尝试提交带有表单操作的表单。 However the submit takes quite a long time, so I'm trying to show a loading message while the form is submitting, telling the user to wait on the screen and they'll be redirected afterwards.但是提交需要很长时间,所以我试图在提交表单时显示加载消息,告诉用户在屏幕上等待,然后他们将被重定向。 The problem is, when I show the loading screen, the form submit no longer works.问题是,当我显示加载屏幕时,表单提交不再有效。 I've narrowed it down due to the fact that the submit button that triggered the event no longer exists, thus it won't submit.由于触发事件的提交按钮不再存在,因此它不会提交,因此我缩小了范围。 Is there a way to show the loading screen and ensure the submit action goes through?有没有办法显示加载屏幕并确保提交操作通过?
handleSubmit = () => {
...
this.setState({isLoading: true});
...
this.formRef.current.submit();
}
<form ref={this.formRef} action="https://www.google.com" method="post">
{isLoading ? (
this.renderLoading()
) : (
<>
<input type="text">input</input>
<button type="submit" onClick={this.handleSubmit}>button<button>
</>
</form>
I have tried the below solution and it works successfully, however, I don't want the button to be shown in the loading screen.我已经尝试了以下解决方案并且它成功运行,但是,我不希望该按钮显示在加载屏幕中。
handleSubmit = () => {
...
this.setState({isLoading: true});
...
this.formRef.current.submit();
}
<form ref={this.formRef} action="https://www.google.com" method="post">
{isLoading ? (
this.renderLoading()
) : (
<>
<input type="text">input</input>
</>
<button type="submit" onClick={this.handleSubmit}>button<button>
</form>
Is there a way to make this work without showing the button in the loading screen?有没有办法在不显示加载屏幕中的按钮的情况下完成这项工作?
Hide the controls rather than killing them:隐藏控件而不是杀死它们:
handleSubmit = () => {
...
this.setState({isLoading: true});
...
this.formRef.current.submit();
}
<form ref={this.formRef} action="https://www.google.com" method="post">
<>
{ isLoading && this.renderLoading() }
<input type="text" className={isLoading ? 'hidden' : ''}>input</input>
<button type="submit" onClick={this.handleSubmit} className={isLoading ? 'hidden' : ''}>button<button>
</>
</form>
css for hidden
class is display: none
hidden
类的 css display: none
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.