Event only fires once when making Ajax request in jQuery

后端 未结 1 657
失恋的感觉
失恋的感觉 2021-01-28 23:43

I have a couple of drop downs that are populated from SharePoint using SPServices. This part works great. But then, I have a button that on click loads data from SharePoint and

相关标签:
1条回答
  • 2021-01-29 00:35

    I would guess that you are replacing the part of the page where the button lives. (you really need to format your code more neatly for SO... use JSFiddle.net and their TidyUp button).

    If that is the case you need to use a delegated event handler:

    $(document).on('click', '.myButton', function () {
        run()
    });
    

    This listens at a static ancestor of the desired node, then runs the selector when the event occurs, then it applies the function to any matching elements that caused the event.

    document is the fallback parent if you don't have a convenient ancestor. Do not use 'body' for delegated events as it has odd behaviour.

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