2

I have this button:

<button type="button" id="topic_schedulati" class="btn btn-info">Mostra Topics Schedulati</button>

This is my jquery code to handle the click:

(function() {
    $(window).on('action:ajaxify.end', function(event, data) {

        if (new RegExp(/^category\/[0-9]+/).test(data.url)) {
            $(document).ready(function(){
            $('body').on('click', '#topic_schedulati', function() {
                console.log("hi");
            });
        });
        }

    });
}());

Why when I click on the button I show the print "hi" two times and not one? Anyone can help me?

4

1 回答 1

1

这个动作“action:ajaxify.end”很可能被多次调用。当您将事件附加到正文时,不需要您的其他条件,因为事件将响应与 id“topic_schedulati”匹配的任何添加元素,请记住,您应该只有 1 个具有该 id 的元素,或者您'会有不稳定的行为,具体取决于浏览器。

$(function() {
    $('body').on('click', '#topic_schedulati', function() {
            console.log("hi");
    });
});
于 2016-09-14T14:10:56.903 回答