2010-08-17 61 views
2

我在fancybox內部遇到jquery問題。基本上我有一個通過ajax渲染的表單,並且它已經加載到fancybox中。但問題是,jquery不能在fancybox加載的表單上工作:(但是,當我轉到實際的表單頁面時,jquery無法正常工作。fancybox內部的jquery無法正常工作

我在這裏發現了類似的問題http://groups.google.com/group/fancybox/browse_thread/thread/fe6f9cad7b42df79他說要使用絕對引用。 ..我甚至試過了,但你沒有運氣。

任何人都面臨着類似的問題?

這裏是我的jQuery代碼


$(document).ready(function() { 
    $("input#user_username").click(function(e){ 
    alert("asaadasd"); 
    }); // this works fine when called through the form loaded in the page, but doesn't when the form is loaded within fancybox 

    $("a#login-link, a#login-link2, a#signup-link, a#signup-link2").fancybox({ 
    'scrolling' : 'no', 
    'titleShow' : false 
    }); 

}); 

任何幫助嗎?

回答

5

如果您的表單是通過ajax加載的,則click事件將不會綁定到document.ready上,因爲它當時並未出現在文檔中。改爲使用live()。這將事件綁定到文檔中的當前和未來元素。

$("input#user_username").live("click", function(e) { 
    alert("asaadasd"); 
}); 
+0

謝謝你的sooo多! :)我根本不知道這個功能...... – Madhusudhan 2010-08-17 23:06:30

0

您應該使用。對()現在(活不推薦)...

$(document).on("click", "input#user_username", function(e) { 
    alert("asaadasd"); 
});