我有一個編輯按鈕,點擊後會觸發下面的第一個事件。它(編輯表單)也有一個取消鏈接,點擊後會觸發下面的第二個事件。如何確保在取消事件中可訪問PrevContent
值?用戶可以一次點擊任意數量的相應編輯鏈接。事件之間通過變量
$('.js-profile-edit').live('click',function(){
var TrBlock = $(this).closest('tr');
var PrevContent = TrBlock.html();
var colspan = TrBlock.parent().prev().find('a:first').first().data('span');
$.ajax({
url : $(this).attr('href'),
type : 'GET',
success : function(response){
TrBlock.html('<td colspan="'+colspan+'">'+response+'</td>');
},
error : function(jqXHR, textStatus, errorThrown){
uiAlert({message : 'Something Broke. Try again later'})
}
});
return false;
});
$('.js-profile-edit-cancel').live('click',function(){
$(this).closest('tr').html(PrevContent);
return false;
});
只是一個脫離主題的筆記,'.liv e()'自從jQuery 1.7以來已被棄用。從那時起,我一直在使用'.delegate()':http://api.jquery.com/delegate/ –
@ spryno724所以委託一個代替實時的下降。我嘗試過,但沒有工作 – aWebDeveloper
對於你的情況,你會使用''('body')。delegate('。js-profile-edit','click',function(){})' –