2012-06-01 45 views
-1

我有一個編輯按鈕,點擊後會觸發下面的第一個事件。它(編輯表單)也有一個取消鏈接,點擊後會觸發下面的第二個事件。如何確保在取消事件中可訪問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; 
}); 
+0

只是一個脫離主題的筆記,'.liv e()'自從jQuery 1.7以來已被棄用。從那時起,我一直在使用'.delegate()':http://api.jquery.com/delegate/ –

+0

@ spryno724所以委託一個代替實時的下降。我嘗試過,但沒有工作 – aWebDeveloper

+0

對於你的情況,你會使用''('body')。delegate('。js-profile-edit','click',function(){})' –

回答

0

你應該能夠在prevContent存儲爲.data()上最接近<tr>

$('.js-profile-edit').live('click',function(){ 

    var TrBlock  = $(this).closest('tr'); 
    var PrevContent = TrBlock.html(); 
    TrBlock.data({contents: PrevContent}); // save it 
    ... 
}) 

$('.js-profile-edit-cancel').live('click',function() { 
    var TrBlock = $(this).closest('tr'); 
    TrBlock.html(TrBlock.data('contents')); 
    return false; 
}); 
+0

不會搞砸HTML – aWebDeveloper

1

也許你可以在東西向PrevContent您的取消按鈕的data object

寫在js-profile-edit單擊處理:

$('.js-profile-edit-cancel').data('PrevContent', PrevContent); 

js-profile-edit-cancel單擊處理程序閱讀:

var PrevContent = $(this).data('PrevContent');