Jquery .on()函数没有将事件处理程序与php动态生成的元素正确绑定


Jquery .on() function doesn't bind the event handler properly with element generated dynamically by php

我有一个调用'generate.php'点击事件的js函数。php返回一个id为"alert"的锚标记。js函数将这个锚标记附加到文档中。另一个js函数作用于这个动态生成的锚。正如标题所说,.on()函数不会在这个锚标记上绑定事件处理程序。请帮我一下……

我的js文件如下

$("#generate").click(function(){       // Working Fine :)
     $.post("generate.php",function(reply){
           $("reply").html(reply);
     }
});    
$("#alert").on("click",function(){     //Not Working :(
     alert("hello");
}

我的html代码如下

<a href="#" id="generate">Click TO generate</a>
<div id = "reply"></div>

我的generate.php是这样的

<?php //Working Fine
echo '<a href="#" id="alert">Click To get alert Box</a>'; 
echo '<div id="abc">Click The above link to get an alert</div>';
?>

委托是这样使用的。请参阅。()

$(document).on("click","#alert",function(){   
     alert("hello");
}); //added missing );

提示:您正在添加带有硬编码ID的元素,这将导致重复元素。