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

后端 未结 5 2026
失恋的感觉
失恋的感觉 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:46

    This is happening because you're adding the input element after you wired up the event. Try .on:

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

    Using .on will make sure the keyup event is wired up to inputs that are on the page originally, as well as any that are added later dynamically.

提交回复
热议问题