繁体   English   中英

如何在react js中加载时提交?

[英]how to form submit on load in react js?

当页面在 ReactJS 中加载时如何提交form

<form id="redirectForm" method="post" action="https://test.cashfree.com/billpay/checkout/post/submit">
  <input type="text" name="appId" value={this.state.appId}/>
  <input type="text" name="orderId" value={this.state.orderId}/>
  <input type="text" name="orderAmount" value={this.state.orderAmount}/>
  <input type="text" name="orderCurrency" value={this.state.orderCurrency}/>
  <input type="text" name="orderNote" value={this.state.orderNote} />
  <input type="text" name="customerName" value={this.state.customerName}/>
  <input type="text" name="customerEmail" value={this.state.customerEmail} />
  <input type="text" name="customerPhone" value={this.state.customerPhone} />
  <input type="text" name="returnUrl" value={this.state.returnUrl} />
  <input type="text" name="notifyUrl" value={this.state.notifyUrl} />
  <input type="text" name="signature" value={this.state.signature}/>
  <button type="submit">Pay</button>
</form>

Javascript:

<script>document.getElementById("redirectForm").submit();</script>

其实可以为表单创建一个ref,如果组件是类组件,可以使用componentDidMount提交表单,如果是功能组件,可以通过useEffectuseEffect

<form ref={item => this.form = item} id="redirectForm" method="post" action="https://test.cashfree.com/billpay/checkout/post/submit"> 
.....
</form>

在您的 React 组件中,您可以使用以下方法创建 ref:

class SampleClass extends React.Component {
   constructor() {
     this.form = null
   }

   componentDidMount() {
    this.form.submit();
   }
}

您可以使用 useRef 和 useEffect 将类似的逻辑应用于功能组件。

更多细节: https : //reactjs.org/docs/refs-and-the-dom.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM