How to grab just one part of a page in jQuery?
I have an form being submitted via AJAX. The response from the server in an entire web page (HTML). I would like to alert only the content within a span tag with the id "message" which exists on that response page. I don't want 开发者_如何学Cto alert the entire page. How to do this?
$.ajax({
url: '/accounts/login/',
data: $(this).serialize(),
success: function(output) {
alert(output)
}
});
My code alerts the whole page.
$.ajax({
url: '/accounts/login/',
data: $(this).serialize(),
success: function(output) {
alert($(output).find('#message').html());
}
});
Try this:
$.ajax({
url: '/accounts/login/',
data: $(this).serialize(),
success: function(output) {
alert($('#message', output).text());
}
});
Easiest way would probably be to add the page to your page within a hidden element and select the span using getElementById. Alternately if it is xhtml, you could load it into an xml document and use xpath. And, possibly the hardest, you could parse the string and find the part that matches up to the html you want.
Or, either of these fine examples :)
精彩评论