开发者

jQuery nested .each() getting outside of the function

开发者 https://www.devze.com 2023-03-09 15:25 出处:网络
I\'m using the code below to save the order of multiple sortable lists via Ajax call, and for some reason it runs the call multiple times per click. I get 4 to 8 alerts if there are two lists in the #

I'm using the code below to save the order of multiple sortable lists via Ajax call, and for some reason it runs the call multiple times per click. I get 4 to 8 alerts if there are two lists in the #resedit div.

I can't understand why the ajax call or alert would be made more than once... the only thing happening in the .each functions is building a variable, and they're fully closed out before anything else occurs.

Can anyone see where I'm going wrong?

var listorder = 开发者_开发知识库'';
    $('#resedit').children().each(function(index) {
        if ($(this).css('display') != 'none' && $(this).attr('id') != '') {
        listorder = listorder + $(this).attr('id') + ', ';
                $(this).children().each(function(indexchildren) {
                    if ($(this).css('display') != 'none' && $(this).attr('id') != '') {
                        listorder = listorder + $(this).attr('id') + ', ';
                        placeholder = indexchildren;
                        }
                    });
        }
        });
    var data = {
        action : 'save_res',
        security : '<?php echo $saveres_nonce; ?>',
        resorder : listorder,
        resumeslug : $('#res-dash-select').val(),
        }
    jQuery.post(ajaxurl, data, function(response) {
        alert(response);
        return;
        });
    });


Your Ajax call is within the (outer) children().each() call. So it will be called once for every immediate child of #resedit div.

And I guess the #resedit div has between 4 and 8 children.

0

精彩评论

暂无评论...
验证码 换一张
取 消