flaskjs form.validate_on_submit()confirm()集成

2024-09-28 05:28:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个表格,在一些价值观审查后写下更改。在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插入一个是/否确认按钮,但我不知道如何在单击后集成“提交停止”

我设法在button元素中集成了一个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整合flask提交

提前非常感谢您的帮助


Tags: ofpyformrest表单ifonannotation
1条回答
网友
1楼 · 发布于 2024-09-28 05:28:08

您可以在单击事件时使用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
  }
});

相关问题 更多 >

    热门问题