开发者

Only replace fragment in jquery ajax call

I'm trying to basically mimic the functionality of load with an ajax request. The reason for this is I need to have access to the beforeSend functionality.

The load that would work for me is

$('tableCell').load('this/is/a/url #table');

This would obviously load the element table into tableCell.

The ajax that I have currently is

$.ajax({
    url: 'this/is/a/url',
    beforeSend: function() {
        //STUFF
    },
    success: function(html) {
        //Not sure what to put here?
    }
});

My question is what do I do in the success function to only load table into table cell.

I've tried things like

$('#tableCell').html(html.getElementById('table'));

but that didn't seem to work, I've also tried some other combinations of js and jquery... but no luck开发者_如何学JAVA.

Hopefully someone can help me out!

Thanks.


This should work: $('#tableCell').html($(html).find('#table'));


You pretty much have it. Double check your selectors to make sure they're matching something; it's unclear reading your examples what the ID of the table actually is.

// append to the table (i think this is how load() works)
$(html).find('#table').appendTo('#tableCell');

or

// replace the data
$('#tableCell').html( $(html).find('#table') );

..assuming #tableCell is in the DOM and #dt_report is in the AJAX response.

Also, if #table is the root node in the variable "html", you will have to use filter() instead of find(), but I don't think that's the case here.


One option would be to actually put the html content you receive from the AJAX call somewhere on your page and hide it and then use the normal jQUery selectors to pick out what you need from the hidden table.

The best case would honestly be to only get what you need in the AJAX call but I know this isn't necessarily always possible.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜