The problem of element acquisition created dynamically by jquery

problem description

A time picker implemented by myself, click the OK button to get the selected time when it pops up. When popping up dynamically, you can customize the pop-up class, so that multiple time selectors can be distinguished. Currently encountered, dynamically added pop-up window, unable to obtain the situation.

effect

Code

$(document).on("click",".layer-btn-ok",function(e){
    let a = $(this).parents(".chuzu-agree-end")
    console.log(a)
})

"chuzu-agree-end"

because each pop-up window, the parent"s class name is customizable. How can I get the [OK] button through a custom class?

Nov.15,2021

if the event is added later, it is best to use an event agent, otherwise you may not be able to trigger the event that you could have clicked normally due to the redrawing and reflow of the page. Now there is a very cool agent directly to the body. If you are interested, you can go to the Internet to have a look.


in fact, jquery's on method is the event agent mechanism. However, it is not easy to act as a proxy for events by dynamically adding elements and then getting dynamic elements. My current solution can only be to modify the plug-in and add it to the button again according to the dynamically added class name. In this way, it can be selected directly based on the class name.
is roughly as follows

Menu