Events triggered by dynamically generated element are not captured by event handler

后端 未结 5 2024
失恋的感觉
失恋的感觉 2020-11-21 05:46

I have a

with id=\"modal\" generated dynamically with the jQuery load() method:

$(\'#modal\').load(\'handl         


        
5条回答
  •  借酒劲吻你
    2020-11-21 06:34

    When you dynamically change the DOM, jQuery won't attach event handlers to them. You need to use on() and delegated events

    For your input items, you'll need something like:

    $("").on("keyup", "input", function() { 
        handler = $(this).val();
        name = $(this).attr('name');
    })
    

    Where the parentSelector is something higher in the DOM than the input element, and an element that exists at page load, maybe the form ID or something.

提交回复
热议问题