简体   繁体   English

在 React 中隐藏提交按钮后如何提交表单

[英]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.

相关问题 提交后如何隐藏表单,目前您必须单击切换按钮 - How to hide a form after submit in react, currently you have to click a toggle button 在javascript中点击“提交”按钮后,如何获取表格div +错误消息的高度? - How do you get the height of a form div + error messages after hitting the submit button in javascript? 你如何使用javascript提交表单<input type=“button”> - How do you submit a form with javascript with a <input type=“button”> 提交后的表单未定义“this”,但反应中的按钮未定义 - “this” is undefined for a form after submit but not for button in react 单击提交反应js后隐藏表单组件 - hiding a form- component after clicking submit react js 如何制作它以便您无法单击“开始”提交表单(iPhone),但必须点击“提交”按钮? - How do I make it so that you cannot click “GO” to submit the form (iPhone), but must tap the “submit” button? 在 Angular 6 中使用反应式表单隐藏字段后如何提交表单? - How to submit a form after hiding a field using reactive form in Angular 6? 提交表单后如何重定向到 React 中的欢迎组件 - After submit the form how do I redirect to welcome component in React 在AngularJS ng-submit中提交表单后,如何绑定视图? - How Do You Bind A View After Form Submit In AngularJS ng-submit? 您如何确定使用jQuery提交表单的提交? - How do you determine which submit was used to submit a form with jQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM