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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:29:20  来源:igfitidea点击:

Replace content in div after successful ajax function

jqueryajax

提问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

http://api.jquery.com/html/

http://api.jquery.com/html/

 $.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 successhandler. 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/