2011-05-29 91 views
0

我使用下面的代码通过Ajax调用保存多个可排序列表的顺序,并且由于某种原因它每次点击多次运行该调用。如果#resedit div中有两个列表,我会收到4到8个警报。jQuery嵌套.each()获取函数的外部

我不明白为什么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; 
     }); 
    }); 
+0

还有一个'});'在最后,这导致我相信这里面有更多的代码。是否还有其他东西,这是包裹在或只是不需要'})'。 – 2011-05-29 20:38:34

回答

0

你的Ajax调用(外)children().each()调用中。所以它会被调用一次#resedit div的每个直系孩子。

我想#resedit div有4至8个孩子。

+0

谢谢你,但我确定它不是。在var data ='之前关闭children()。each()'调用为了增加神秘感,似乎迭代的次数与通过#resedit div更新的次数直接相关.html()与来自其他函数的ajax响应。第一次两次,然后三次,然后四次...... – 2011-05-29 16:57:32

+0

你有'children()。each()'两次。正如你所说,内部的一个在'var data ='之前关闭。但是外部代码在粘贴代码的末尾处关闭。 – Codo 2011-05-29 17:35:06