成功执行ajax功能后替换div中的内容

时间:2011-12-06 17:15:07

标签: jquery ajax

我希望在ajax功能成功后替换<div id="container"></div>的内容,也无需刷新页面。

$.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;

2 个答案:

答案 0 :(得分:11)

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

 $.ajax({
  type: "POST",
  url: "scripts/process.php",
  data: dataString,
  success: function( returnedData ) {
    $( '#container' ).html( returnedData );
  }
 });

也使用http://api.jquery.com/load/

$( '#container' ).load( 'scripts/process.php', { your: 'dataHereAsAnObjectWillMakeItUsePost' } );

答案 1 :(得分:2)

您可以为ajax请求设置“context”,然后将其传递到success处理程序。在那里,您可以致电.html()来替换内容。

$.ajax({
  type: "POST",
  url: "scripts/process.php",
  data: dataString,
  context: '#container',
  success: function() {
    $(this).html('<p> Your article was successfully added!</p>');
  }
});

参考:

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