从AJAX(jQuery)加载模式窗口时的绑定元素 [英] Binding elements when modal window loads from AJAX (jQuery)

查看:118
本文介绍了从AJAX(jQuery)加载模式窗口时的绑定元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在模式窗口内设置一个拖放列表.窗口的内容是通过AJAX调用加载的,所以我认为我需要使用jQuery的.live()方法.

I'd like to set up a drag'n'drop list inside a modal window. The window's content is loaded via an AJAX call, so I believe I need to use jQuery's .live() method.

这是我当前的代码:

$('#cboxLoadedContent').live('load', function() {
    // Event for sortable page lists
    $('ul#pageList').sortable();
});

当模式窗口加载时,如何设置绑定?模态窗口来自colorbox插件.

How can I set up bindings when the modal window loads? The modal window comes from the colorbox plugin.

注意:我猜想'load'是不适合使用的事件,因为如果我在其中扔一个简单的alert(),则即使在窗口加载时它也不会显示.

Note: I'm guessing that 'load' is not the right event to use, because if I throw a simple alert() in there, it doesn't even show up when the window loads.

推荐答案

在您的情况下,通过ajax加载元素时,只需在元素上调用.sortable()即可,

In your case, just call .sortable() on the elements when you're loading them via ajax, like this:

$.ajax({
   url: 'page.html',
   //stuff...
   success: function(data) {
     //Put stuff in DOM
     $('ul#pageList').sortable();
   }
});

对于为什么部分:.live()不能按这种方式工作,它不等待新元素并执行操作...它侦听使DOM冒泡的元素中的事件如果选择器匹配,则执行事件处理程序.

For the why part: .live() doesn't work this way, it doesn't wait for new elements and do things...it listens for events from elements that bubble up the DOM then executes event handlers if it has a selector that matches.

这篇关于从AJAX(jQuery)加载模式窗口时的绑定元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆