Adding handler to form inside div, in the future

邮差的信 提交于 2019-12-12 05:28:03

问题


I am using the following code to direct the results from a form to a specific div.

$(window).load(function () {
  $("#form1").submit(function() {
      $.post($(this).attr("action"), $(this).serialize(), function(html) {
        $("#resultsDiv").html(html);
      });
     return false; // prevent normal submit
  });
});

How can I apply this (or any) handler to future forms that may be created within an updated div ( with new yet to created content inserted into the div at some point in the future)?

I have looked at the .on but I do not see an event for the updating or reloading of a div.

I have tried adding a similar function to the above, but replacing (window) with ("#thefutureDivID"), but no luck.


回答1:


The best place to add the handler is right after you know the element exists. So, right after this line:

$("#resultsDiv").html(html);

you can add your code that references $("#thefutureDivID").



来源:https://stackoverflow.com/questions/8203771/adding-handler-to-form-inside-div-in-the-future

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!