【问题标题】:jQuery nested .each() getting outside of the functionjQuery 嵌套 .each() 超出函数
【发布时间】:2011-09-04 08:25:47
【问题描述】:

我正在使用下面的代码通过 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;
        });
    });

【问题讨论】:

  • 末尾有一个额外的});,这让我相信其中包含更多代码。是否还有其他东西包含在内或者只是不需要})

标签: javascript jquery nested each


【解决方案1】:

您的 Ajax 调用在(外部)children().each() 调用中。所以它会为#resedit div 的每个直接子级调用一次。

我猜#resedit div 有 4 到 8 个孩子。

【讨论】:

  • 谢谢你,但我确定不是。 The children().each() 调用在 var data = 之前关闭 为了增加谜团,似乎迭代次数与通过 .html() 使用来自的 ajax 响应更新#resedit div 的次数直接相关其他功能。第一次两次,然后三次,然后四次,等等......
  • 你有两次children().each()。正如你所说,内部的在var data = 之前关闭。但外层在粘贴代码的最后关闭。
猜你喜欢
  • 2012-08-15
  • 2011-11-19
  • 1970-01-01
  • 2015-01-25
  • 2013-03-15
  • 2010-10-12
  • 1970-01-01
  • 2015-05-24
  • 1970-01-01
相关资源
最近更新 更多