event fired by a future loaded element [duplicate]

陌路散爱 提交于 2019-12-13 09:45:15

问题


I have a form thats loads an image. I need to do a function that is been called by the image Onclick Event.

The problem is that the selector ( I'm using jQuery) can't find the image id, because when the function is loaded the image wasn't yet there.

SOLUTION :

After the image upload -->

$("#"+foto_nom).bind({
       click:function(){
       alert(this.id); }
 });

Binding an event is the solution.


回答1:


You should use 'on' method to attach event to selectors that are not present during page load. Like this:

   $(document).on('click', '#id_of_your_image', function(e) {
    //your code here
    });

update

Binding event to the image after it has been added does not seem to be a good solution.



来源:https://stackoverflow.com/questions/17407129/event-fired-by-a-future-loaded-element

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