嗨,我有表列出所有註冊用戶。我有兩個選項掛起/反應和刪除。我希望在提交這些按鈕之前顯示一條確認消息,以便在處理這些按鈕時警告用戶會發生什麼情況。所以我決定使用一個叫做jquery.confirm的插件,它的功能非常出色。我有這個功能的設置,我從文檔了:jquery確認在表中提交正確的表格
// SUSPEND USER
$(".suspend-user").confirm({
text: "Are you sure you want to suspend this user? Login access for this user will be removed.",
title: "Confirmation required",
confirm: function(button) {
$('.suspend-user').closest('form').submit();
},
cancel: function(button) {
// nothing to do
},
confirmButton: "Yes I'm sure.",
cancelButton: "No.",
post: true,
confirmButtonClass: "btn-danger",
cancelButtonClass: "btn-green"
});
所以一旦用戶已經確認這他們會找到最接近的形式,並提交。但是,它會提交頁面上的最後一個表單,而不是最接近的表單,或者該按鈕實際上鍊接的表單。這是使用此代碼實現的:
confirm: function(button) {
$('.suspend-user').closest('form').submit();
},
有沒有一種方法可以鎖定正確的表單?我的HTML如下:或查看JSFiddle:
大加讚賞
<table id="userTable" class="table">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>User Name</th>
<th>Email</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>user</td>
<td>user</td>
<td>useruser</td>
<td>[email protected]</td>
<td> <a href="http://myapp.local/admin/user/1/edit"><span class="icon-edit"></span>Edit</a>
</td>
<td>
<form method="POST" action="http://myapp.local/admin/user/1/delete" accept-charset="UTF-8">
<input name="_method" type="hidden" value="PATCH">
<input name="_token" type="hidden" value="cHO3sLY9zjBUfI1J60AShfOeThxQbOVFbDyhpuQo">
<button type="submit" class="btn btn-danger suspend-user"><span class="icon-remove-user"></span>Suspend</button>
</form>
</td>
<td>
<form method="POST" action="http://myapp.local/admin/user/1/removeUser" accept-charset="UTF-8" class="delete-user-profile" id="delete-userform">
<input name="_method" type="hidden" value="DELETE">
<input name="_token" type="hidden" value="cHO3sLY9zjBUfI1J60AShfOeThxQbOVFbDyhpuQo">
<button type="submit" class="btn btn-danger delete-user dialog-confirm"><span class="icon-trash"></span>Delete</button>
</form>
</td>
</tr>
<tr>
<td>P</td>
<td>b</td>
<td>user</td>
<td>[email protected]</td>
<td></td>
<td>
<form method="POST" action="http://myapp.local/admin/user/2/restore" accept-charset="UTF-8">
<input name="_method" type="hidden" value="DELETE">
<input name="_token" type="hidden" value="cHO3sLY9zjBUfI1J60AShfOeThxQbOVFbDyhpuQo">
<button type="submit" class="btn btn-green restore-user"><span class="icon-add-user"></span> Re-activate</button>
</form>
</td>
<td>
<form method="POST" action="http://myapp.local/admin/user/2/removeUser" accept-charset="UTF-8" class="delete-user-profile" id="delete-userform">
<input name="_method" type="hidden" value="DELETE">
<input name="_token" type="hidden" value="cHO3sLY9zjBUfI1J60AShfOeThxQbOVFbDyhpuQo">
<button type="submit" class="btn btn-danger delete-user dialog-confirm"><span class="icon-trash"></span>Delete</button>
</form>
</td>
</tr>
<tr>
<td>wegrerhb</td>
<td>bkhbjhb</td>
<td>bkjbk</td>
<td>[email protected]</td>
<td></td>
<td>
<form method="POST" action="http://myapp.local/admin/user/28/restore" accept-charset="UTF-8">
<input name="_method" type="hidden" value="DELETE">
<input name="_token" type="hidden" value="cHO3sLY9zjBUfI1J60AShfOeThxQbOVFbDyhpuQo">
<button type="submit" class="btn btn-green restore-user"><span class="icon-add-user"></span> Re-activate</button>
</form>
</td>
<td>
<form method="POST" action="http://myapp.local/admin/user/28/removeUser" accept-charset="UTF-8" class="delete-user-profile" id="delete-userform">
<input name="_method" type="hidden" value="DELETE">
<input name="_token" type="hidden" value="cHO3sLY9zjBUfI1J60AShfOeThxQbOVFbDyhpuQo">
<button type="submit" class="btn btn-danger delete-user dialog-confirm"><span class="icon-trash"></span>Delete</button>
</form>
</td>
</tr>
<tr>
<td>erhreth</td>
<td>rher</td>
<td>erheh</td>
<td>[email protected]</td>
<td> <a href="http://myapp.local/admin/user/29/edit"><span class="icon-edit"></span>Edit</a>
</td>
<td>
<form method="POST" action="http://myapp.local/admin/user/29/delete" accept-charset="UTF-8">
<input name="_method" type="hidden" value="PATCH">
<input name="_token" type="hidden" value="cHO3sLY9zjBUfI1J60AShfOeThxQbOVFbDyhpuQo">
<button type="submit" class="btn btn-danger suspend-user"><span class="icon-remove-user"></span>Suspend</button>
</form>
</td>
<td>
<form method="POST" action="http://myapp.local/admin/user/29/removeUser" accept-charset="UTF-8" class="delete-user-profile" id="delete-userform">
<input name="_method" type="hidden" value="DELETE">
<input name="_token" type="hidden" value="cHO3sLY9zjBUfI1J60AShfOeThxQbOVFbDyhpuQo">
<button type="submit" class="btn btn-danger delete-user dialog-confirm"><span class="icon-trash"></span>Delete</button>
</form>
</td>
</tr>
</tbody>
@espascarello這個工程!謝謝。我認爲我的方法會將最後一種形式看作是測試所顯示的那種形式,但是現在就感謝它的作用 – 001221 2015-02-11 16:54:20