javascript - 在 WordPress 中的 async/await/then Javascript 块中提交评论表单
问题描述
我有一个异步函数来验证表单的输入。如果一切正确,则应提交表格。
我尝试了以下代码,我的目标是调用异步函数,一旦完成,我将执行“then”部分,根据异步函数分类的结果,我想完成表单提交。
除了commentForm.submit()的实际代码外,一切都运行良好(原始提交事件的拦截、对异步函数的调用、评论文本的评估……)我得到一个Uncaught (in promise) TypeError: commentForm.submit is not a function error
我想我错过了一些非常明显的东西,但此时我被阻止了。
<script>
window.onload=function() {
var commentForm = document.getElementById('commentform');
commentForm.addEventListener('submit', function(event){
async function classify(input) {
// checks, for simplificity let's assume we just return false
return false;
}
event.preventDefault();
const textComment = document.getElementById('comment').value;
classify([textComment]).then(result => {
if (result) {
alert('Your comment has been flagged and cannot be submitted');
}
else {
alert('Good to go');
commentForm.submit();
}
})
})
}
</script>
解决方案
推荐阅读
- fastlane - Android 用户名和密码的 Fastlane 配置
- flutter - Flutter Web - 如何在桌面上的 Chrome 中测试 Flutter Web 应用程序打开?
- html - 如何正确地将 3 个 div 对齐为一行中的圆圈?
- prisma - 当 Prisma 中的字段更新时,将字段增加到唯一整数
- java - 希望并排显示 2 个 JOptionPanes
- django - 如果没有向用户显示,在 Django 模型中使用 IntegerChoices 字段是否有优势?
- javascript - 如何使用数组更改 html 元素属性
- css - 显示顺风动画
- ssl - 带有自签名 ssl 证书的 prometheus 的 Https
- svelte - 如何在 SvelteKit 中添加自定义 404 页面和不同的错误页面(针对其他错误)?