Rebinding events in jQuery after Ajax update (updatepanel)

后端 未结 9 2158
悲哀的现实
悲哀的现实 2020-11-27 10:15

I have several input and option elements on my page, each (well almost) have an event attached to update some text on the page once they change. I use jQuery which is really

9条回答
  •  不知归路
    2020-11-27 10:54

    As of jQuery 1.7, the recommended way to do this is to use jQuery's .on() syntax.

    Make sure, however, that you set up the event on the document object, not the DOM object itself. For example, this will break after the UpdatePanel postback:

    $(':input').on('change', function() {...});
    

    ... because the ':inputs' have been rewritten. Do this instead:

    $(document).on('change', ':input', function() {...});
    

    As long as the document is around, any inputs (including those from UpdatePanel refreshes) will trigger the change handler.

提交回复
热议问题