2012-11-09 34 views
1

我正在關注@coreyward的示例,用於將對象編輯窗體置於模態Rails中的對話窗口,它是outlined in this answerupdated for rails 3.1 in this gistjQuery「對象函數(e,t){返回新v.fn.init(e,t,n)}沒有方法'on'

我正在使用Rails 3.2.8和jQuery-Rails 2.1.3(jQuery 1.8.2是在應用程序中加載的東西)。然而,這個要點的咖啡文字在第7行(在評論之後)引發了標題中的錯誤。

$ -> 
    $modal = $('#modal') 
    $modal_close = $modal.find('.close') 
    $modal_container = $('#modal-container') 

    # Handle modal links with the data-remote attribute 
    $.on 'ajax:success', 'a[data-remote]', (xhr, data, status) -> 
    $modal 
     .html(data) 
     .prepend($modal_close) 
     .css('top', $(window).scrollTop() + 40) 
     .show() 
    $modal_container.show(); 

    $.on 'click', '#modal .close', -> 
    $modal_container.hide() 
    $modal.hide() 
    false 

this question發表的評論我這一個jQuery版本的問題可能是原因,這就是爲什麼我檢查了我的jQuery版本,但顯然這jQuery是不一樣古老,其解決了這個問題的人。我還驗證了jQuery在該腳本之前加載(或者至少在加載順序中位於該腳本的上方)。

我把上面的咖啡腳本放到a fiddle which demonstrates the problem(參見你的控制檯的錯誤)。 (這也讓我覺得這不是我的設置的問題。)我會嘗試不同版本的jQuery,看看他們是否解決了這個問題,但也許代碼只是靠譜?我不太瞭解coffeescript,看不到其他人可能會遇到的錯誤。

回答

3

更換$.on$(document).on刪除錯誤...的on方法需要從我可以告訴一個DOM元素上調用,從來沒看到過所謂的jQuery的根對象之前...

+2

我會實際上更新了我的觀點 - 我的觀點,因爲我沒有複製粘貼代碼。 – coreyward

相關問題