2011-12-06 54 views
2

我想在ajax函數成功後替換<div id="container"></div>的內容,也沒有頁面刷新。ajax函數成功後替換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; 

回答

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' }); 
2

您可以設置,然後傳遞到success處理Ajax請求的「背景」。在那裏,您可以撥打.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/