jquery inside fancybox not working

时光毁灭记忆、已成空白 提交于 2019-12-09 22:58:43

问题


I am facing problems with jquery within fancybox. Basically I have a form rendered through ajax and its been loaded within fancybox. But the problem is, jquery doesn't work on the form loaded by fancybox :( but it works fine when i go to the actual form page.

I found a similar problem here http://groups.google.com/group/fancybox/browse_thread/thread/fe6f9cad7b42df79 where he says to use absolute referencing... I even tried that, but no luck.

Anybody of you have faced a similar problem?

here is my jquery code


$(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
  });

});

any help?


回答1:


If your form is loaded via ajax, the click event will not be bound to it on document.ready since it's not present in the document at that time. Use live() instead. This binds the event to both present and future elements in the document.

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



回答2:


You should use .on() now (as live is deprecated)...

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


来源:https://stackoverflow.com/questions/3505695/jquery-inside-fancybox-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!