Ajax 更新后重新绑定 jQuery 事件 (updatepanel)
我的页面上有几个输入和选项元素,每个元素(几乎)都附加了一个事件,用于在页面上的某些文本发生更改时更新它们。 我使用 jQuery,这真的很酷:)
我还使用 Microsoft 的 Ajax 框架,利用 UpdatePanel。 我这样做的原因是,某些元素是根据某些服务器端逻辑在页面上创建的。 我真的不想解释为什么我使用 UpdatePanel - 即使它可以(可以通过相当多的努力)重写为仅使用 jQuery 我仍然想要那个 UpdatePanel。
您可能猜到了 - 一旦我在 UpdatePanel 上进行回发,jQuery 事件就会停止工作。 我实际上期待这一点,因为“回发”并不是真正的新回发,因此我在 document.ready 中绑定事件的代码不会再次被触发。 我还通过阅读 jQuery 帮助库证实了我的怀疑。
无论如何,在 UpdatePanel 完成更新 DOM 后,我留下了重新绑定控件的问题。 我最好需要一个不需要向页面添加更多 .js 文件(jQuery 插件)的解决方案,但需要像能够捕获 UpdatePanel 的“afterupdating”一样简单的解决方案,我可以在其中调用我的方法来重新绑定所有表单元素。
I have several input and option elements on my page, each (well almost) have an event attached to update some text on the page once they change. I use jQuery which is really really cool :)
I also use Microsofts Ajax framework, utilizing the UpdatePanel. The reason why I do that is that certain elements are created on the page based on some server-side logic. I don't really want to explain why I use the UpdatePanel - even if it could (it can with quite some effort) be rewritten to use only jQuery I still want that UpdatePanel.
You probably guessed it - once I have a postback on the UpdatePanel the jQuery events stops working. I actually was expecting this, since the "postback" is not really a new postback so my code in document.ready that binds the events won't be fired again. I also confirmed my suspicion by reading up on it in the jQuery help libraries.
Anyway I'm left with the problem of rebinding my controls after the UpdatePanel is done updating the DOM. I preferably need a solution that does not require adding more .js files (jQuery plug-ins) to the page but something as simple as being able to catch the UpdatePanel's 'afterupdating' where I can just call my method to rebind all the form elements.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
由于您使用的是 ASP.NET AJAX,因此您可以访问
pageLoad
事件处理程序,每次页面回发时都会调用该事件处理程序,无论是从 UpdatePanel 回发完整页面还是部分页面。 您只需将该功能放入您的页面即可,无需连接。Since you're using ASP.NET AJAX, you'll have access to a
pageLoad
event handler, that gets called each time the page posts back, be it full or partial from an UpdatePanel. You just need to put the function in to your page, no hooking up is required.或者您可以通过
on()
检查最新的 jQuery 实时功能方法。Or you could check the latest jQuery's live functionality via the
on()
method.从 jQuery 1.7 开始,推荐的方法是使用 jQuery 的 .on() 语法。
但请确保您在 document 对象上设置事件,而不是在 DOM 对象本身上。 例如,这将在 UpdatePanel 回发后中断:
...因为 ':inputs' 已被重写。 相反,请执行以下操作:
只要文档存在,任何输入(包括来自 UpdatePanel 刷新的输入)都将触发更改处理程序。
As of jQuery 1.7, the recommended way to do this is to use jQuery's .on() syntax.
Make sure, however, that you set up the event on the document object, not the DOM object itself. For example, this will break after the UpdatePanel postback:
... because the ':inputs' have been rewritten. Do this instead:
As long as the document is around, any inputs (including those from UpdatePanel refreshes) will trigger the change handler.
使用以下代码
Use following code
您可以使用 jQuery 和事件委托。 基本上将事件挂钩到容器而不是每个元素,并查询 event.target 并基于该目标运行脚本。
它有多种好处,可以减少代码噪音(无需重新绑定)。 这是
也更容易占用浏览器内存(DOM 中绑定的事件更少。)
简单示例此处。
jQuery 插件,用于轻松事件委托。
PS 我 99% 确信代表团将在下一个版本中出现在 jQuery 核心中。
You could use jQuery and event delegation. Basically hook events to containers rather than every element and query the event.target and run script based on that.
It has multiple benefits in that you reduce the code noise (no need to rebind). It is
also easier on browser memory (less events bound in the DOM.)
Quick example here.
jQuery plugin for easy event delegation.
P.S I am 99% sure delegation will be in the jQuery core at the next release.
使用以下代码,您需要验证控件将使用datapicker:
Use the following code, You need to validate the control will use the datapicker:
在“if”中,您可以放入每次 updatepanel 执行 AsyncPostBack 时需要执行的代码。
into of the "if" you can put the code that you need execute every time that the updatepanel does AsyncPostBack.
使用 jQuery 的新“live”方法绑定您的事件。 它将事件绑定到所有当前元素以及所有未来元素。 查清! :)
Bind your events using jQuery's new 'live' method. It will bind events to all your present elements and all future ones too. Cha ching! :)