Php ajax将html注入div,但保留包含其id的html元素
我使用一些ajax调用一个php文件,该文件返回一些html(一个图像和几个按钮),然后将其内容放入一个div中。问题是,我希望能够使用php返回的其中一个按钮的id来连接事件处理程序。如果我在浏览器中查看source,则source的输出仅显示html注入的div,而不是html:Php ajax将html注入div,但保留包含其id的html元素,php,ajax,jquery,Php,Ajax,Jquery,我使用一些ajax调用一个php文件,该文件返回一些html(一个图像和几个按钮),然后将其内容放入一个div中。问题是,我希望能够使用php返回的其中一个按钮的id来连接事件处理程序。如果我在浏览器中查看source,则source的输出仅显示html注入的div,而不是html: <div class="displaysomething"></div> } PHP-最终返回一个按钮和其他东西。这就是我需要根据事件处理程序的id连接到事件处理程序的内容 echo '&
<div class="displaysomething"></div>
}
PHP-最终返回一个按钮和其他东西。这就是我需要根据事件处理程序的id连接到事件处理程序的内容
echo '<input id="stop-service" type="button" value="Run" class="'.$strRunActionButtonClass.'"/>';
echo';
如果我只是在页面上放置一个按钮,而没有使用AJAX将其注入到div中,那么我的按钮连接代码将非常有效
有人有什么想法吗
感谢jQuery中的
。单击(…
添加事件处理程序的方法只会将事件添加到现有元素中。以后添加的新元素不包括在内
您可以使用事件绑定的方法来包括以后添加的元素
$("body").on("click", "#stop-service", function(e){
e.preventDefault();
runHDIMService();
});
我创建了一个。问题是
$(document).ready(function () {
getServiceDisplay();
$('#stop-service').click(function(e) // this doesn't exists yet
{
e.preventDefault();
runHDIMService();
});
这应该起作用:
函数getServiceDisplay(){
太好了!我一直在想这些问题,但我打算等一等/睡一觉,这可能不管用,而且非常粗鲁。非常感谢你抽出时间来帮助我。
$("body").on("click", "#stop-service", function(e){
e.preventDefault();
runHDIMService();
});
$(document).ready(function () {
getServiceDisplay();
$('#stop-service').click(function(e) // this doesn't exists yet
{
e.preventDefault();
runHDIMService();
});
$.ajax(
{
url: 'includes/dosomething.php',
type: 'POST',
success: function(strOutput)
{
$(".displaysomething").html(strOutput);
// so after you attached the div from ajax call just register your event
$('#stop-service').click(function(e)
{
e.preventDefault();
runHDIMService();
});
});
};