简体   繁体   English

异步数据加载后提交HTML表单的正确方法是什么?

[英]What is the proper way to submit HTML form after asynchronous data load?

Silly that I can't find this so I apologize in advance if I have duplicated, but I have looked through search engines and SO without finding what I need. 很抱歉,我找不到这个,所以如果我重复了的话,我会提前道歉,但是我一直在搜索引擎和搜索引擎,但没有找到我需要的东西。

What is the proper way to submit an HTML form upon user click but only after I have asynchronously loaded data from a 3rd party API to fill in for some of the form's hidden fields? 仅当我从第三方API异步加载数据以填写表单的某些隐藏字段之后,才可以在用户单击时提交HTML表单的正确方法是什么?

I tried this structure, which has not worked: 我尝试了这种结构,但没有用:

form onclick = function1()
function1() calls function apicall that retrieves info from 3rd party api and in its completion handler then calls function2
function2 checks all form fields and returns true if form should be submitted, false if it should not

With this structure, my form is always submitted, although both the 3rd party api function completion handler and function2 are working properly on their own. 通过这种结构,尽管第3方api函数完成处理程序和function2都可以正常工作,但始终会提交我的表单。 function2 can return false all day long but the form still submits. function2可以整天返回false,但表单仍会提交。 What can I do about this? 我该怎么办? The asychronous timing seems alright but somehow the return false is not getting back to the FORM element. 异步计时似乎还不错,但是以某种方式返回false并不会返回到FORM元素。

Thank you for any tips or code samples. 感谢您提供任何提示或代码示例。

Asynchronous functions can't return anything -- the caller isn't waiting for them to finish. 异步函数无法返回任何内容-调用者无需等待它们完成。 So the form's onsubmit handler should return false to prevent the form from submitting. 因此,表单的onsubmit处理程序应return false以防止表单提交。

Then in function2() , if the form validation succeeds, it can call 然后在function2() ,如果表单验证成功,则可以调用

document.getElementById('formID').submit();

to perform the actual form submission. 执行实际的表单提交。

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

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