Jquery click event not firing on previously hidden div

后端 未结 3 660
星月不相逢
星月不相逢 2021-01-27 10:29

I am loading a \'password-reset-success-message\' div into a \'password-reset\' div dynamically as shown in the below snippet.The \'password-reset-success-message\' div

相关标签:
3条回答
  • 2021-01-27 11:09

    delegate it to the parent element you are adding the html to

    if using jQuery 1.7+ with .on()

    $("#password-reset").on('click','#btnPasswordResetOK',function(){
    
    })
    

    with delegate

    $("#password-reset").delegate('#btnPasswordResetOK','click',function(){
    
    })
    

    From jQuery docs on which method to use

    $(selector).live(events, data, handler); // jQuery 1.3+

    $(document).delegate(selector, events, data, handler); // jQuery 1.4.3+

    $(document).on(events, selector, data, handler); // jQuery 1.7+

    By delegating, you bind the event handler to a static parent element that doesn't change and exists at the time of binding. You specify the element and the events you want to listen to, and when the event bubbles up it will get handled by the static parent element.

    0 讨论(0)
  • 2021-01-27 11:11

    Try using 'live'

    $(document).on(events, selector, data, handler);  
    
    0 讨论(0)
  • 2021-01-27 11:13
    $(selector).live(events, data, handler);                // jQuery 1.3+
    $(document).delegate(selector, events, data, handler);  // jQuery 1.4.3+
    $(document).on(events, selector, data, handler);        // jQuery 1.7+
    

    Your content is not there on the page when ready event is triggered. You generate the elements and content dynamically so you must use live/on event handler.

    0 讨论(0)
提交回复
热议问题