flask-js form.validate_on_submit() confirm() integration



我有一个表单可以在一些值审查后编写更改。routes.py,我确保表单通过以下.validate_on_submit()进行验证:

### ... rest of routes.py ....
form = VariantOverallRevision()
if form.validate_on_submit() :
### ... code to write new class on file ...
flash("sent!")
### ... rest of routes.py ....

我想通过js在此提交中插入是/否确认按钮,但我不知道单击后如何集成"提交停止"。

我设法在按钮元素中集成了一个confirm(),但显然表单会在单击按钮后立即发送(而不是在调用 jsconfirm()之后(:

function confirm_ACMG_review()
{
confirm("Save data?");
}
if (confirm_ACMG_review) {
//proceed to sumbmission
}
else {
//block submission
}
document.getElementById('manual_ACMG_annotation_confirm_review').onclick = confirm_ACMG_review;

这里是HTML部分:

<form id="manual_ACMG_annotation_confirm_form" action="" method="post" novalidate>
{{ form.hidden_tag() }}
{{ form.submit( id = "manual_ACMG_annotation_confirm_review", style = "font-size: 120%; font-weight: bold;" ) }}
</form>

我知道我可以打开不同的页面/重定向到其他 URL 等,但我想知道是否有任何方法可以使用 js 进行烧瓶提交。

提前非常感谢任何帮助!

您可以在单击事件中使用preventDefault()方法,以便默认情况下不提交表单。

document.getElementById("manual_ACMG_annotation_confirm_review").addEventListener("click", function(event){
event.preventDefault() // prevent default form submission
// here you can implement confirmation
// set confirm_ACMG_review value based on confirmation result
if (confirm_ACMG_review) {
document.getElementById('manual_ACMG_annotation_confirm_form').submit();  // submit form manually
}
});

最新更新