2011-04-07 36 views
1

我有表讓您可以在可編輯的窗體和值之間切換。我也有一個讓我動態添加表克隆的功能。我需要能夠在新創建的表上使用相同的切換功能。試圖使用.live與.toggle

我嘗試使用.live功能進行切換,但這使得我必須在可編輯窗體出現之前進行額外的點擊。我如何解決這個問題,所以我只需要點擊一次?

$('.block a.submit').live('click', function(){ 
    $('.block a.submit').toggle(
     function(){ 
      $(this).text('Save').parent().each(function(){ 
       $(".value", this).hide(); 
       $(".edit", this).show(); 
       $(this).find('.edit :first').focus(); //focuses on first form element - less clicks 
       $('thead').show(); 
      }); 
     }, 
     function(){ 
      $(this).text('Edit').parent().each(function(){ 
       $(".edit", this).hide(); 
       $(".value", this).show(); 
       $('thead').hide(); 
      }); 
     } 
    ); 
}); 

下面是我試圖完成的一個粗略的模擬。唯一的主要區別是表單最初是隱藏的。 http://jsfiddle.net/z5C2F/

回答

1

試試這個:

$('.block a.submit').live('click', function(){ 
    $(this).toggle(//you need to use this 
     function(){ 
      $(this).text('Save').parent().each(function(){ 
       $(".value", this).hide(); 
       $(".edit", this).show(); 
       $(this).find('.edit :first').focus(); //focuses on first form element - less clicks 
       $('thead').show(); 
      }); 
     }, 
     function(){ 
      $(this).text('Edit').parent().each(function(){ 
       $(".edit", this).hide(); 
       $(".value", this).show(); 
       $('thead').hide(); 
      }); 
     } 
    ); 
}); 
+0

仍需要額外的點擊 – Justin 2011-04-07 17:03:37

+0

你可以發佈一些對烏爾代碼的jsfiddle,這是很難沒有任何標記 – Neal 2011-04-07 17:04:03

+0

肯定的是,你還需要什麼呢? – Justin 2011-04-07 17:07:08