2011-07-24 87 views
0

我有一个单击处理程序,它读取标记的href属性并通过ajax加载新内容。该函数返回false,因此它不会遵循href url。这只会工作一次,但每次之后,函数都不会被调用,并且内容不是异步加载的,而是在浏览器中的链接之后。jQuery ajax有一次工作,但每次都通过浏览器加载href

$ ("document").ready(function() { 
      $(".post_update").click(function() { 
       $("#dashboard_content").html(ajax_load).load($(this).attr('href')); 
       return false; 
      }); 
}); 

<a href="{{ path('post_update', {"id":post.id}) }}" class="post_update">Edit</a> 
+0

什么是ajax_load? – ShankarSangoli

+0

ajax_load只是字符串“Loading ...”而.post_update在#dashboard_content内,是 – ThinkingInBits

+0

这是什么“{{path('post_update',{”id“:post.id})}}”要做什么? – ShankarSangoli

回答

3

您不应该使用文档作为字符串它的对象本身尝试贝尔代码。

由于链接位于仪表板容器内部,因此您应该在此情况下使用live。

$(document).ready(function() { 
      $("a.post_update").live('click', function() { 
       $("#dashboard_content").html(ajax_load).load($(this).attr('href')); 
       return false; 
      }); 
}); 
+0

的格式打印出URL的分支函数。我改变了它以便文档不是字符串,但是这仍然只能用于第一个时间。 – ThinkingInBits

+0

立即尝试我编辑了我的答案,我不知道链接是在容器内。 – ShankarSangoli

+0

谢谢你。 .live做到了。 – ThinkingInBits

0

如果.post_update里面#dashboard_content,问题是到事件处理程序绑定到的元素,现在已经不复存在了。最简单的解决方案是使用jQuery.live方法。因此,您的代码如下所示:

$(document).ready(function() { 
    $(".post_update").live("click", function (e) { 
     $("#dashboard_content").html(ajax_load).load($(this).attr('href')); 
     return false; 
    }); 
}); 
相关问题