2011-07-24 34 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; 
    }); 
}); 
相關問題