简体   繁体   English

升级的jQuery,.on()和document.ready不能与UpdatePanels一起使用

[英]Upgraded jQuery, .on() and document.ready not working with UpdatePanels

I've seen some of the other solutions here on StackOverflow, but I'm particularly curious as to why this isn't working all of the sudden. 我已经在StackOverflow上看到了其他一些解决方案,但我特别好奇为什么这不会突然发生。 A little background... we are still using jQuery 1.7.1, and we are attempting to patch up plugins and pages that do not work with the newer version of jQuery. 一点背景......我们仍在使用jQuery 1.7.1,我们正在尝试修补不适用于较新版本jQuery的插件和页面。 At the moment, I'm testing with 1.8.3. 目前,我正在测试1.8.3。 Here's the issue: 这是问题所在:

I have a grid in an UpdatePanel with buttons in a TemplateField. 我在UpdatePanel中有一个网格,其中包含TemplateField中的按钮。 Each button has a class assigned to it, and when clicked... pops up a modal. 每个按钮都有一个分配给它的类,当点击时...弹出一个模态。 This worked perfectly under 1.7.1 where I put the .live event in the document.ready. 这完全在1.7.1下工作,我把.live事件放在document.ready中。 Upgrading to 1.8.3, I got rid of live() and use on(), all in the document.ready. 升级到1.8.3,我摆脱了live()并使用on(),所有这些都在document.ready中。

Whenever I page on the grid, an async postback fires and the .on() is lost. 每当我在网格上页面时,异步回发都会触发,而.on()会丢失。 The only change I made was upgrading to 1.8.3. 我做的唯一改变是升级到1.8.3。 and changing live() to on(). 并将live()更改为on()。 This previously worked. 这之前有用。 Why all of the sudden can I not do this. 为什么突然间我不能这样做。 Example code below: 示例代码如下:

$(document).ready(function () {
            $("#AppDialog").dialog({
                bgiframe: true,
                autoOpen: false,
                height: 400,
                width: 650,
                modal: true,
                resizable: false }
            });

            $(".AppEdit").on("click", function (e) {
                e.preventDefault();
                $("#AppDialog").dialog('open');
            });
});

Button is inside a TemplateField in a grid that is in an UpdatePanel: Button位于UpdatePanel中的网格中的TemplateField内:

<asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="EditApp" CssClass="AppEdit" />

Any ideas why this would be? 任何想法为什么会这样? I don't want to use pageLoad, so what is my best option here? 我不想使用pageLoad,那么这里最好的选择是什么?

The difference between .live() and .on() is that .live() will apply to all elements (including those added in the future with ajax), whereas .on() only applies to elements currently in the DOM. .live()和.on()之间的区别在于.live()将应用于所有元素(包括将来使用ajax添加的元素),而.on()仅适用于当前在DOM中的元素。

$(document).on('click', '.AppEdit', function() {});

What this does is set an event handler to the document object and whenever an event bubbles up and is caught, it will check whether the eventObject.target has the class of AppEdit, and if so, execute your function. 这样做是为文档对象设置事件处理程序,每当事件冒泡并被捕获时,它将检查eventObject.target是否具有AppEdit类,如果是,则执行您的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM