问题描述
|
我正在使用以下代码通过Ajax调用保存多个可排序列表的顺序,由于某种原因,它每次点击都会多次运行该调用。如果#resedit div中有两个列表,我会收到4到8个警报。
我不明白为什么多次执行ajax调用或警报... .each函数中唯一发生的事情是构建变量,并且在发生其他任何事情之前它们已完全关闭。
谁能看到我要去哪里错了?
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;
});
});
解决方法
您的Ajax呼叫在(外部)
children().each()
呼叫之内。因此,每个ѭ2every div的直接子对象都将被调用一次。
而且我猜#resedit
div有4到8个孩子。