2011-09-19 53 views
2

我有一個名單上的一個按鈕,觸發此事件:使追加發生僅在第一次

JS:

$(this).parents('tr') 
     .find('.class') 
     .append("<input type='text' size='5'/>"); 

它可能使這一追加在第一時間剛剛發生的呢?

像一個return false,不知道。

謝謝!

+0

我看到了這個以前的帖子:http://stackoverflow.com/questions/4115951/jquery-run-only-once那是你在找什麼? – MRR0GERS

回答

6

假設此行爲發生在click事件上,可以使用one。例如,

$('#my-button').one('click', function(e) { 
    e.preventDefault(); 
    $(this).parents('tr') 
     .find('.class') 
     .append("<input type='text' size='5'/>"); 
}); 


編輯:使用 delegate

var selector = '#my-button', 
    handler = function(e) { 
     // prevent default behavior 
     e.preventDefault(); 
     // make sure this only runs once 
     $(document).undelegate(selector, 'click', handler); 
     // actual behavior 
     $(this).parents('tr') 
      .find('.class') 
      .append("<input type='text' size='5'/>"); 
    }; 
$(document).delegate(selector, 'click', handler); 
+0

會試試這個,結果是brb。 –

+0

你能告訴我這與live()一起工作嗎? –

+0

你能告訴我如何用live()完成這項工作嗎? ** –