我想用修改后的AJAX响应将id中的内容替换为id = test。
javascript:
('.test').bind('ajax:success', function()
{
$.ajax({
success: function(html){
$('.test').text($(html).find("#test")); // returns [object Object]
}
});
});
但是$(html).find("#test")返回了[object Object],$(html).find("#test").html()返回了null——如何在ajax响应中获取ID为“ test”的div内容?
Based on this blog link, the answer is in the second parameter:
jQuery(function($) {
// create a convenient toggleLoading function
var toggleLoading = function() { $("#loading").toggle() };
$("#tool-form")
.bind("ajax:loading", toggleLoading)
.bind("ajax:complete", toggleLoading)
.bind("ajax:success", function(event, data, status, xhr) {
$("#response").html(data);
});
});
Then, your code would be:
$('.test').bind('ajax:success', function(event, data, status, xhr) {
// this is $('.test')
$(this).text($(data).find("#test"));
});