简体   繁体   English

jQuery事件丢失在ajax加载的内容

[英]jquery event loss on ajax loaded content

Let's say that I have some HTML content in which I have some textareas. 假设我有一些HTML内容,其中有一些文本区域。 This HTML content is loaded through Ajax after performing some kind of action (like a button click or something). 执行某种操作(例如单击按钮或执行某种操作)后,将通过Ajax加载此HTML内容。 The problem that I have is that I lose all kinds of jQuery events that were bond to my textareas (like the .change event or something, so if I do something like: .change的问题是我丢失了与我的文本区域绑定的所有jQuery事件(例如.change事件之类的东西,所以如果执行以下操作:

$('.my_textarea').change(function(){
    alert('TEST');
});

My alert does not display because my textarea has been loaded with Ajax. 我的警报未显示,因为我的文本区域已加载Ajax。 Does anybody have any idea why this problem happens and how I can fix this? 有人知道为什么会发生此问题以及如何解决此问题吗?

Thanks in advance for any suggestions 在此先感谢您的任何建议

$(document).on('change', '.my_textarea', function(){
    alert('TEST');
});

Just replace document with the closest static ( not loaded via Ajax ) parent of your textarea 只需用textarea的最接近的父级(不通过Ajax加载)替换document

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

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