【发布时间】:2021-04-18 20:16:07
【问题描述】:
我正在使用 wtforms,这是我的提交按钮:
{{ form.submit(class="btn btn-outline-info") }}
这是我的 onbeforeunload 函数:
<script type="text/javascript">
window.onbeforeunload = function() {
return "Are you sure you want to navigate away?";
}
</script>
我知道在普通的 html 表单中我可能可以使用 jquery 来执行此操作:
参考:
jquery-function-before-form-submission stackoverflow discussion
clear onbeforunload stackoverflow discussion
<head>
<script src="jquery-3.5.1.min.js"></script>
</head>
<script>
$('#myform').submit(function(event) {
event.preventDefault(); //this will prevent the default submit
window.onbeforeunload = null;
$(this).unbind('submit').submit(); // continue the submit unbind preventDefault
})
<script>
当我使用这些 wtforms 对象时我应该怎么做? (ps我不熟悉js或jquery....请指出我做错了什么......)
提前致谢!
【问题讨论】:
-
我怀疑你需要打电话给
preventDefault()。如果您打算以任何方式提交。我只是将onbeforeunload设置为 null 并返回 true(返回 true 让提交继续)。
标签: javascript html jquery