Jquery:Selector找不到类?

我正在尝试推进Jquery-autcomplete函数。 我希望Jquery-autocomplete在表中创建新行。 这到目前为止工作。 但我希望Jquery添加一个删除按钮。 因此,用户可以删除其中一个添加的项目。

$(document).ready( function() { //create an new   in #log function log( message ) { $(" " + message + "" + "").appendTo("#log"); $( "#log" ).attr( "scrollTop", 0 );} // get the values from a json-source $( "#REMOTE" ).autocomplete({ source: "json.php", minLength: 0, select: function( event, ui ) { log( ui.item ? ui.item.value + " aka " + ui.item.id : "Nothing selected, input was " + this.value );} }); // this is just a test if jquery recognizes the click $('.Entf').click(function(event){ alert("Thanks for visiting!"); }); }); 

但是jQuery不会识别它创建的元素的点击。 为了检查fpr erros,我在html中放了一行。 这个按钮有效。 但是当我通过Jquery添加一行时,添加的按钮不起作用。 这是一个来自firebug的例子:

 
test //this one workes fine. it comes from the original html
daniel aka 121 //this one doesn't work. it's the one that was added by Jquery

有任何想法吗?

谢谢! 好好用我的英文:)

使用jQuery live() :

 $('.Entf').live('click',function(event){ alert("Thanks for visiting!"); }); 

使用live使jQuery能够在由javascript动态创建的选择器上使用回调

因为在绑定处理程序之后创建了元素,所以它不会注册。 你需要这样的东西:

 $("body").delegate(".Entf", "click", function() { //function here }); 

在jQuery 1.7+中使用jQuery on()

 $(document).on("click",".Entf",function(event){ alert("Thanks for visiting!"); }); 

在jQuery 1.4.3+中使用jQuery delegate() :

 $(document).delegate(".Entf", "click", function(event){ alert("Thanks for visiting!"); }); 

并在jQuery 1.3中使用jQuery live():

 $('.Entf').live('click',function(event){ alert("Thanks for visiting!"); }); 

生成内容后,需要绑定按钮,即在生成内容后,执行以下操作:

  $('.Entf').click(function(event){ alert("Thanks for visiting!"); }); 

再次。

编辑:正如Neal所指出的,做这个方法时要小心。 原因是它将绑定到所有.Entf元素,而不仅仅是新元素,导致多个点击事件。 有关如何创建和绑定事物而不必处理live()或多个绑定的示例,请参见http://jsfiddle.net/R9Kb4/2/ 。