简体   繁体   中英

How to Put an Event Listener on Newly Added Forms submission

I have some forms in my webpage with a class name post_reply

I am using the following to track the submission of the form

$(document).ready(function(){
    $(".post_reply").submit(function(event){
       // statements
    });
});

It works, But If I add some new forms to the page with jquery / javascript, it does not track their submission.

I also tried to

$(document).ready(function(){
    $("#comments").on('submit', 'form.post_reply', function (event) {
      // statements
    });
}); 

In the above #comments is the container div. But it also does not work. Any Solution??

.on() should work. But here's another option:

$("body").delegate("form.post_reply", "submit", function() {
  // statements
);

But the .on() method should really work.

By trying so many ways, finally the following method solved my problem.

$("#comments").on('submit', '.post_reply', function (event) {

});

Thanks to All For Help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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