返回同一页面时,Jquery Stop事件处理程序再次绑定

时间:2021-02-20 19:44:32

SongPage view i have following Jquery code:

SongPage视图我有以下Jquery代码:

$(document).on("click", ".foo", function () {
    changeSong();
});

 function changeSong() {
            $.ajax({
                url: path,
                method: "GET",
                success: function (data) {
                    $('#body-wrapper').html(data);
                }
            });
 }

The changeSong function returns the SongPage view again, because of this it binds another click function again, I can use .one() to make sure it only executes each one once, but is there a way I can do this without creating a new click handler each time?

changeSong函数再次返回SongPage视图,因为它再次绑定了另一个click函数,我可以使用.one()来确保它只执行一次,但是有一种方法我可以在不创建新的点击的情况下执行此操作处理程序每​​次?

1 个解决方案

#1


0  

Yes, there is, you can call .off() before calling .on(). But that does not seem very clean to me.

是的,你可以在调用.on()之前调用.off()。但这对我来说似乎不太干净。

Cannot you just not put the script all over again in the page ? Like doing a non-ajax request for example ?

难道你不能在页面中重新编写脚本吗?就像做一个非ajax请求一样?

#1


0  

Yes, there is, you can call .off() before calling .on(). But that does not seem very clean to me.

是的,你可以在调用.on()之前调用.off()。但这对我来说似乎不太干净。

Cannot you just not put the script all over again in the page ? Like doing a non-ajax request for example ?

难道你不能在页面中重新编写脚本吗?就像做一个非ajax请求一样?