【问题标题】:jQuery ajax problem with div refresh and new contentdiv刷新和新内容的jQuery ajax问题
【发布时间】:2010-02-09 07:05:26
【问题描述】:

我想用发送到 calendar.php 的新内容刷新#calendar div
我收到了成功提醒,但内容并不令人耳目一新。如何让新的 calendar.php 与 POST 数据一起显示?

$(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 -->

【问题讨论】:

    标签: php jquery ajax


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      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!");
      
          });
      
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-31
        • 2012-09-16
        • 1970-01-01
        • 2011-06-03
        • 1970-01-01
        相关资源
        最近更新 更多