jQuery的.load()函数中断从.load开封后模态()完成函数、模态、jQuery、load

2023-09-10 18:30:55 作者:直到死亡把我们分开

我使用jQuery的 .load() 方法和加载页面片段的。下面是我目前的code:

I am using jQuery's .load() method and Loading Page Fragments. Below is my current code:

$("#submit").click(function() {
    $("#results").load('/ajax/box/ #box');
});

这code是一个MODA,当 a.ajax模态点击时调用。问题是,当 .load()完成后, a.ajax模态 #box 被装入 #results 工作不......不不开的模式,而是只是打开了新的一页。它看起来像 #box 没有绑定/连接到任何事件处理程序的内容。

This code is in a moda, which is called when a.ajax-modal is clicked. The problem is that when .load() has completed, the a.ajax-modal in #box that was loaded into #results is not working... it doesn't not open the modal but instead just opens a new page. It seems like the contents of #box are not bound/attached to any event handlers.

下面是正在发生的事情的概要:

Here is an outline of what is happening:

加载页 点击 a.ajax模态和模态节目 在模式提交表单,运行。点击()模态功能 在 #results 内容的网页刷新 点击新 a.ajax模态这是在 #results 加载,但语气不显示 Load page Click on a.ajax-modal and modal shows Submit form in modal, run .click() function in modal Content in #results on page refreshes Click on new a.ajax-modal that was loaded in #results but modal does not show

为什么模态不开放后 .load()完成?

Why is the modal not opening after .load() is complete?

推荐答案

没有事件已被添加到您的元素,因为事件被添加在文档已准备就绪。您需要使用。对()的jQuery方法prevent这一点。

No event has been added to your element because events are added when the document is ready. You need to use .on() method of jQuery to prevent this.

$(function()
    $(document).on('click', 'a.ajax-modal', function() {
       // Your code here.
    });
});

因此​​事件被添加到文件而不是元素本身。