4

こんにちは。フォーム action="POST" が実行される前に、中間関数を実行しようとしています。最初に onSubmit={functionName} という 2 つのことを試しましたが、onSubmit 関数で false を返しても、フォームは常にアクションを実行します。次に、送信イベントをプログラムでディスパッチする代わりに Ref を使用しようとしましたが、何も起こりませんか? 投稿されるフォーム値の構成を取得するには、基本的にサーバー側の呼び出しを行う必要がありますが、残念ながら、使用する外部 API では、この方法でフォームを送信する必要があります。どんな助けでも大歓迎です。

試行 1:

const performSubmit =() => {
 //Using onSubmit and then only performing the return after the axios call is done
 axiosCallFunction.then((res) => {
   setConfig(res.data)
   return true
 }).catch((error)) => {
   //Dont submit form
   return false
 })
}

return (
<form name="form" onSubmit={performSubmit} id="x1" method="POST" action="https://url.aspx" target="_top">
    <input value={config.param}/>
    <button type="submit"> Submit</button>
</form>)

試行 2

const formEl = useRef();

const performSubmit =() => {
 //Currently not calling the submit on the form
 formEl.current.dispatchEvent(new Event("submit"))
}

return (
<form name="form" ref={formEl} id="x1" method="POST" action="https://url.aspx" target="_top">
    <input value={config.param}/>
    <button onClick={performSubmit} />
</form>)

基本的に、フォームを送信する前、またはフォームのアクションを実行する前に、サーバーを呼び出して結果を取得したいと考えています。

4

3 に答える 3

6

やってみました:

formEl.current && formEl.current.submit();

?

于 2020-07-21T13:52:55.253 に答える