jQuery nested .each() getting outside of the function
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.
精彩评论