我有一張客戶表,每行都有一個方法,彈出一個內聯表單,顯示客戶的詳細信息。在這個頁面上還有一個搜索,一些標籤和一些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