简体   繁体   中英

Unable to manipulate HTML elements using jQuery on pjax requests

I have this working fine when loading the page normally (visiting the page via the addressbar):

$(function () {
    $("#verify").click(function (event) {
        event.preventDefault();
        // ...

But if I navigate to the page (that contains the button: id="verify") via pjax calls ( https://github.com/defunkt/jquery-pjax ), the JS code doesn't see the button.

It works if I hit refresh.

How can I overcome this problem?

It is because when you use pjax, you are dealing with dynamically created elements, in such cases you need to use event delegation

Try

$(function () {
    $(document).on('click', "#verify", function (event) {
        event.preventDefault();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM