Jquery on (’click’) does not start after the first click

I have some HTML:

Profile Picture

And with that, I have some Jquery:

$('.posts-content').on('click', '.post-delete', function(e) {
                $('.post-dropdown').dropdown();
                if (confirm('Are you sure you want to delete this post?')) {
                    var $this = $(this);
                    $.ajax({
                        url: '/a/delete_post',
                        type: 'POST',
                        dataType: 'json',
                        data: {'p' : post_id},
                        success: function() {
                            //...
                            return true;
                        }
                    });
                }
                return false;
            });

Everything works perfectly the first click, but if I go to another post, and try to click .post-delete
, the event never fires, and nothing shows up in the console. It also works if I use $.click()
, but I need to dynamically create elements. Why does the $.on('click')
not fire the second click?

My guess is that dropdown plugin you use change the DOM structure.

$('.post-dropdown').dropdown();

BTW,

$this.parent().parent().parent().parent().parent()
this is really really bad practice.

You should use closest
instead.

Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 前端开发 » Jquery on (’click’) does not start after the first click

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录