简体   繁体   English

用ajax更新内容后,jQuery单击事件停止工作

[英]Jquery click event stops working after content is updated with ajax

My application crashes when I try to hide an element which has dynamic content (ie. users list) 当我尝试隐藏具有动态内容(例如,用户列表)的元素时,我的应用程序崩溃

the element i'm trying to show and hide looks like this: 我试图显示和隐藏的元素看起来像这样:

<a href="#" class="group">Users</a>
<ul id="staff">
 <li>User1</li>
 <li>user2</li>
</ul>

The jquery code I'm using is: 我正在使用的jQuery代码是:

$('.group').click(function(e){
 e.preventDefault();
 $(this).siblings('ul').toggleClass('hidden');
});

Users are updated frequently with an ajax call ( the li has a lot more content than just a name). 用户经常通过ajax调用进行更新(li不仅具有名称,而且具有更多的内容)。

I've been searching for answers but none of the results seem to fix my problem, I've tried using several ways of hiding/unhiding the element, using $(document).on('click', '.group', function(){}) instead of .click() , disabling the .click() event untill the ajax call has completed, even with a timeOut of 3sec.. 我一直在寻找答案,但结果都没有解决我的问题,我尝试过使用几种隐藏/ $(document).on('click', '.group', function(){})隐藏元素的方法,使用$(document).on('click', '.group', function(){})而不是.click() ,请禁用.click()事件,直到ajax调用完成为止,即使timeOut为3sec。

this is my Ajax, using ajaxQueue plugin 这是我的Ajax,使用ajaxQueue插件

 working = true;
$.ajaxQueue({
    type: 'post',
    dataType: 'json',
    url: './api/users'
}).done(function(r)
{
            $('#staff').empty(); 
    $.each(r, function(i)
    {
        var addnew = $('<li>').addClass('ind-bg');
  var newDiv = $('<div>').addClass('ind').appendTo(addnew);
    $('<img>').attr('src', r[i].picture).addClass('userPicture').appendTo(newDiv);
    $('<i>').addClass('batch status'+r[i].status).attr('data-icon', '').appendTo(newDiv);
    $('<span>').html(r[i].firstName + ' ' + r[i].name).appendTo(newDiv);
    addnew.appendTo($('#staff'));
    })
setTimeout(function(){working = false}, 3000);
})

Any help or ideas would be greatly appreciated... :) 任何帮助或想法将不胜感激... :)

$(function () {
    $(document).bind("ajaxSend", function (e) {
        e.preventDefault();
        //make what you want to hide
    });
    $(document).bind("ajaxStop", function () {
       //make what you want to show
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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