Jquery .change() function not working with dynamically populated SELECT list

前端 未结 7 895
灰色年华
灰色年华 2020-12-05 14:13

I have a select field that is dynamically populated with an ajax call that simply returns all the HTML select options. Here is the part of the PHP that just echo\'s the sel

相关标签:
7条回答
  • 2020-12-05 14:37

    For items that are dynamically inserted into the DOM, their events must be bound with

    $('.selector').bind('change',function() { doWork() });
    

    This is because when you run $(function(){}); and bind events using $.click or $.change, etc., you are binding events to elements already existing in the DOM. Any manipulation you do after that does not get affected by whatever happens in the $(function(){}); call. $.bind tells your page to look for future elements in your selector as well as current ones.

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