gpt4 book ai didi

javascript - JS : How to use return type of callback function for returning the main callee function

转载 作者:行者123 更新时间:2023-11-28 04:47:30 27 4
gpt4 key购买 nike

我发现了一些好的链接(How to return value from an asynchronous callback function?Returning a value from callback function in Node.js等),但他们无法为我的问题提供解决方案。

我的问题:能够获取异步调用的结果,但如何使用此结果返回我的函数?

onsubmit = "return checkForm(function(callBackResponse) { alert(callBackResponse) });"

此处获取 callBackResponse 的值为 true 或 false,并希望将此值用作:

onsubmit = "return responseFromCallBack;"

已编辑:这是我的异步内容

function checkForm(callback) {return firstCallBack(function secondCallBack(response) {
if (some conditions) {
response = false;
callback(response);
}
else {
response = true;
callback(response);
}
}
);
}

最佳答案

如果我正确地理解了你的问题:
您想在提交之前“验证”(checkForm())吗?!
在本例中,为表单提交事件设置一个事件监听器...
阻止 (event.preventDefault()) 默认行为(提交)调用您的函数 (checkForm()) 如果一切正常,请手动触发提交;)
例如document.getElementById('myFormId').submit()

Cheerio:)

关于javascript - JS : How to use return type of callback function for returning the main callee function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43230966/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com