How do I use JQuery load to retrieve two sets of data in a single go
I'm using the JQuery load method to retrieve an HTML fragment from an external page. I then immediately call that page again to retrieve a different fragment. This works, but seems wasteful.
Can someone suggest a better way?
$('#basket').load('loadCart.php #cartContents', function() {
$("#basket").slideDown开发者_如何学C("slow");
});
$('#cartBar p').load('loadCart.php #cartHeading');
Can you not just use the "," operator in the selector to select multiple elements?
$('#basket').load('loadCart.php #cartContents,#cartHeading', function() {
$("#basket").slideDown("slow");
});
The way I get around this problem uses the $.ajax method and involves retrieving the whole html response. I parse the html using $(r) so that jquery can use it's usual selector functions. The I stored the parts I want in variables and place them into the corresponding container elements.
$.ajax({
type:'post',
url:'yourFile.php',
success: function(r){
var element1 = $(r).find('#element1').html();//get the contents of #element1
var element2 = $(r).find('#element2').html();//get the contents of #element2
$('#container1').html(element1);//put html in container1
$('#container2').html(element2);//put html in container2
}
});
Hope this helps mate!
W.
精彩评论