jQuery .ajax成功函数无法使用jQuery UI元素呈现html

如何使用jquery从.ajax渲染中将html加载到我的div中? success函数加载HTML,但这些元素不会显示为jQuery UI元素,只显示静态HTML类型。 有什么指针吗?

$(function() { $('input[type=image]').click(function(){ $.ajax({ url: '_includes/callinfo.php', data: 'id=' + $(this).attr('value'), dataType: "html", success: function(html){ $('#callwindow').html(html); } }); }); 

});

我需要做的是首先将数据(html)引入指定的div,然后初始化html中每个元素的插件。 喜欢如此:

 $(function() { $('input[type=image]').click(function(){ $.ajax({ url: '_includes/callinfo.php', data: 'id=' + $(this).attr('value'), dataType: "html", success: function(html){ $('#callwindow').html(html); // Initialize UI Items $("input:submit", ".callsubmit").button(); $("#hotorcold").buttonset(); $("#calldatep").datepicker(); $("#callbackdatep").datepicker(); } }); }); 

});

服务器上的MIME类型可能未设置为text/html jQuery检查mime类型以处理来自服务器的数据。 我见过你手动设置dataType

分析回应

检查您的回复是否不是HTML编码,因此<>变为<gt; 分别。 如果它是正常的,那么将HTML视为字符串而不是实际的HTML元素。

这只是我的猜测因为我没有看到你的完整代码:)

如果在代码中添加另一行,如下所示:

 $(function() { $('input[type=image]').click(function(){ $.ajax({ url: '_includes/callinfo.php', data: 'id=' + $(this).attr('value'), dataType: "html", success: function(html){ $('#callwindow').html(html); } return false; //here is the line }); }); 

这个额外的行是为了防止html默认操作(当你点击输入时,有点像这样)。 可能有帮助因为我遇到类似的情况,回调函数不起作用…

jQuery UI元素仅应用于原始DOM文档。 即使在加载文档之后,您也需要将这些UI转换应用于添加到DOM的任何元素。 假设我正在使用jQuery可选UI组件 。

 $(function(){ // Initialize the selectable plugin on all elements with the selectable class $('.selectable').selectable(); // Whenever this button is clicked, it adds a new div to the body // with the selectable class $('input[type=button]').click(function(){ $('body') .append("
New Div
") // Add the new div to the DOM .children('.selectable:last') // Traverse the body's children and grab the last child (the one we just added to the DOM) .selectable(); // Initialize the plugin on the newly added div }); });

您将需要类似于click处理程序内部代码的成功回调。