jQuery ajax功能成功后替换div中的内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8403953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replace content in div after successful ajax function
提问by Ilja
I would like to replace a content of <div id="container"></div>
after the ajax function is successful, also without page refresh.
我想<div id="container"></div>
在ajax功能成功后替换一个内容,也没有页面刷新。
$.ajax({
type: "POST",
url: "scripts/process.php",
data: dataString,
success: function() {
//Whats the code here to replace content in #conatiner div to:
//<p> Your article was successfully added!</p>
}
});
return false;
回答by Dan Heberden
$.ajax({
type: "POST",
url: "scripts/process.php",
data: dataString,
success: function( returnedData ) {
$( '#container' ).html( returnedData );
}
});
also using http://api.jquery.com/load/,
还使用http://api.jquery.com/load/,
$( '#container' ).load( 'scripts/process.php', { your: 'dataHereAsAnObjectWillMakeItUsePost' } );
回答by jAndy
You can set the "context" for an ajax request which then is passed into the success
handler. In there, you might just call .html()
to replace the content.
您可以为 ajax 请求设置“上下文”,然后将其传递给success
处理程序。在那里,您可能只是调用.html()
来替换内容。
$.ajax({
type: "POST",
url: "scripts/process.php",
data: dataString,
context: '#container',
success: function() {
$(this).html('<p> Your article was successfully added!</p>');
}
});
Ref.:
参考:
http://api.jquery.com/jQuery.ajax/
http://api.jquery.com/html/
http://api.jquery.com/jQuery.ajax/
http://api.jquery.com/html/