jQuery - 防止提交未保存的表单
我有一个客户表,每一行都有一个方法可以弹出内联表单,显示客户详细信息。此页面上还有搜索、一些选项卡以及一些 href 链接和链接。功能。
我想要做的是阻止用户将该表单置于未保存状态。所以我在表单输入更改或粘贴时将变量 formChanged 设置为 TRUE。 IF formChanged == true 那么我不希望用户能够单击链接或执行搜索,直到他们确认是否保存数据。
我使用以下内容来提示用户:
$(document).click(function(eve) {
if (!$(eve.target).is('#form *')) {
if (formChanged) {
if (confirm("Do you want to save the data or continue to edit?")) {
$('#form').submit();
return true;
}
else { return false; } //here I do not want to submit the form OR continue with the openForm() method;
}
}
});
html 标记(简化)是这样的:
<a href="/new">New Customer</a>
<a href="javascript:performSearch();">Search</a>
<table>
<tr>
<td onclick="openForm(1);">Customer One</td>
</tr>
<tr>
<td onclick="openForm(2);">Customer Two</td>
</tr>
<tr>
<td><form> //form gubbins loaded via ajax in here </form></td>
<tr>
<td onclick="openForm(2);">Customer Two</td>
</tr>
</table>
我遇到的问题是我可以捕获确认逻辑,但其他功能如 openForm & PerformSearch() 仍然继续执行。
理想情况下,我只想在用户选择“保存”时运行功能/跟随 URL,如果用户按“取消”则不执行任何操作。
显而易见的解决方案是将此逻辑放在 openForm 和 PerformSearch 函数中,但这是一个过于简单化的示例,因为真实页面有数十个不同的函数,这些函数在许多页面上共享,因此这实际上是不可能的
I have a table of customers, each row has a method which pops down an inline form, displaying the customers details. Also on this page is a search, some tabs, and some href links & functions.
What I am trying to do is prevent the user from being able to leave this form in an unsaved state. so I have a variable formChanged set to TRUE on form input change or paste. IF formChanged == true then I do not want the user to be able to click on a link, or perform a search until they have confirmed whether or not to save the data.
I am using the following to prompt the user:
$(document).click(function(eve) {
if (!$(eve.target).is('#form *')) {
if (formChanged) {
if (confirm("Do you want to save the data or continue to edit?")) {
$('#form').submit();
return true;
}
else { return false; } //here I do not want to submit the form OR continue with the openForm() method;
}
}
});
The html markup (simplified) is like this:
<a href="/new">New Customer</a>
<a href="javascript:performSearch();">Search</a>
<table>
<tr>
<td onclick="openForm(1);">Customer One</td>
</tr>
<tr>
<td onclick="openForm(2);">Customer Two</td>
</tr>
<tr>
<td><form> //form gubbins loaded via ajax in here </form></td>
<tr>
<td onclick="openForm(2);">Customer Two</td>
</tr>
</table>
The problem I am having is I can capture the confirmation logic, but the other functions like openForm & performSearch() still continue to execute.
Ideally I would like to ONLY run the functions / follow the URL if the user selects SAVE, and do nothing if the user pressed CANCEL.
The obvious solution is to put this logic in openForm and performSearch functions, but this is a grossly oversimplified example, as the real page has dozens of different functions which are shared on many pages and thus this is not really possible
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能想要这样的东西: Javascript - 离开页面时确认
而不是
if(disabledConfirm_exit) return;
在代码的最后一段中,检查formChanged
变量。在 jQuery 中 - 你可以使用这个: http://api.jquery.com/unload/
You probably want something like this: Javascript - Confirmation when leaving page
Instead of the
if(disabledConfirm_exit) return;
in the last section of code, check theformChanged
variable.In jQuery - you can use this: http://api.jquery.com/unload/
更新
我不知道是否有更好的模式或一些现有的方法可供您使用,但在有人发布更好的答案之前,这就是我所拥有的。您可以编写一个委托方法,该方法将在任何地方调用,并且作为参数,将被赋予要调用的函数和该函数的参数。它可以检查表单是否已更改 - 如果没有更改,它将调用适当的函数,否则它将提示用户。像这样的东西:
check
函数可能看起来像这样(在它的当前形式中,它非常简单,并且仅适用于将调用的函数的一个参数,但它可以变得更通用):您可以如果您通过 JavaScript 附加事件处理程序而不是在 HTML 中指定它们,那么可能会这样做更干净的方式 - 将标记与行为分离的不引人注目的 JavaScript 的原则
听起来您正在寻找window.onbeforeunload
Update
I don't know if there's a better pattern or some existing method that you might be able to use but until someone posts a better answer, here's what I have. You could write a delegating method that will be called everywhere and as arguments, will be given the function to be invoked and the arguments for that function. It can check if the form has been changed - if not, it will invoke the appropriate function otherwise it'll prompt the user. Something like this:
The
check
function could look like this (in it's current form it's really simplistic and only works with one argument for the function that will be invoked but it could be made more generic):You could probably do this much cleaner fashion if you attached your event handlers via JavaScript rather than specifying them in the HTML - the tenet of unobtrusive JavaScript of separating the markup from the behaviour
It sounds like you're looking forwindow.onbeforeunload