我有一张客户表,每行都有一个方法,弹出一个内联表单,显示客户的详细信息。在这个页面上还有一个搜索,一些标签和一些href链接&函数。jQuery - 防止提交未保存的表单
我想要做的是防止用户能够将此表单保留在未保存的状态。所以我有一个变量formChanged设置为TRUE表单输入更改或粘贴。如果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如果用户选择保存,并且如果用户按下CANCEL,则不执行任何操作。
显而易见的解决办法是把在的OpenForm和performSearch功能这个逻辑,但是这是一个非常简化的示例,作为真正的页面有几十种不同的功能,这是在许多网页共享,因此,这是不是真的有可能
可是如何才能让。卸载在showForm()函数中执行,类似于这样的操作。$('#form')。remove(); //卸载应该在这里调用,但似乎没有? $('tr#id')。load('getform.php');' – danielc 2011-05-20 02:20:21