使用div刷新和新内容的jQuery ajax问题

时间:2010-02-08 23:00:20

标签: php jquery ajax

我想用发送到calendar.php的新内容刷新#calendar div 我收到了成功警报,但内容并不令人耳目一新。如何使用POSTed数据显示新的calendar.php?

$(document).ready(function(){
 $('#next').click(function() {
  $.ajax({
       type: "POST",
       url: "calendar.php",
       data: 'year=2012',
       target: '#calendar',
       success: function(html){
          alert("Success");

       }
     });
   });

相关的HTML代码在这里: #next是calendar.php

中div标记的ID
<div id="calendar">
    <?php include('calendar.php'); ?>
</div><!-- /#calendar -->

2 个答案:

答案 0 :(得分:2)

您可以在处理程序中明确设置它:

success : function (html) {
    $('#calendar').html(html);
}

此外,由于您将替换日历div(包含#next链接)的内容,因此您可能需要使用.live('click', function() {而不是.click(function(),因为你将失去事件处理程序。

答案 1 :(得分:0)

jQuery提供了以下更简单的方法:

$('#next').click(function(){

    $('#calendar').load('calendar.php', {year: 2012}, function(){

        // The response html was loaded into the calendar div already
        // You could use this area to display a success message
        alert("Done!");

    });

});
相关问题