【发布时间】:2017-05-31 08:15:25
【问题描述】:
我正在使用 primefaces 开发几个 GUI,我想在离开网站之前警告用户。我在这里找到了一个完美的解决方案: How to detect unsaved data in form when user leaves the page? 这是我正在使用的 JavaScript 代码(在链接中找到)
$(function() {
// Set the unload message whenever any input element get changed.
$(':input').on('change', function() {
setConfirmUnload(true);
});
// Turn off the unload message whenever a form get submitted properly.
$('form').on('submit', function() {
alert('I am here');
setConfirmUnload(false);
});
});
function setConfirmUnload(on) {
var message = "You have unsaved data. Are you sure to leave the page?";
window.onbeforeunload = (on) ? function() {
return message;
} : null;
}
我的问题是我的 primefaces 保存按钮是 ajax 并且不调用“提交”操作。并且用户将在保存页面后被提醒他将离开网站。 commandButton的代码如下:
<p:commandButton value="save" id="saveButton" validateClient="true" actionListener="#{myView.save}" update="description name" style="float:right"> </p:commandButton>
我尝试在 ajax 调用中添加“执行表单”:
<p:commandButton value="save" id="saveButton" validateClient="true" actionListener="#{myView.save}" update="description name" style="float:right">
<p:ajax execute="@form">
</p:commandButton>
但它也没有工作。 如果按钮设置为 $ajax="false"$ 一切正常,但我想避免它。 任何人都可以帮忙吗? 非常感谢!
【问题讨论】:
标签: javascript jsf primefaces