0

すべての「li」要素をループしてデータを収集する機能があります。同様に、すべてのデータを配列にプッシュしています。すべての 'li' ループが終了したら、更新されたデータで関数を 1 回呼び出す必要がありますか?

私の現在のコードは次のようになっています: しかし、3 倍の条件を取得するため、3 倍の関数を呼び出します。関数は正常に動作します。関数を呼び出すのは間違っています。すべてのループを終了した後、一度呼び出すようにするための良い提案はありません。

var pieIndex = [];
$('button.pieDraw', $(accordionBox)).live('click', function () { //done button
    var uls = $(accordionSec.accorGeography).find('ul');

    $.each(uls, function (i, val) {
        pieIndex = []; //i am clearing the array each time to get new value
        var currentLI = $(val).find('li');
        $(currentLI).each(function () {
            if ($(this).hasClass('c_on')) { // this is finding the class 3 times. it's fine
                var functionName = pieFunction[$(this).parent().attr('title') + 'Pie'].fun;
                pieIndex.push($(this).attr('data-index')); // at the last i am getting 1,2,3 it's correct.
                generatePieURL(functionName, curQI, crrentMonth, currentYear, pieIndex);
                //it is calling 3 times. i need only one time to call..
            }
        })

    })

});

前もって感謝します。

4

2 に答える 2

1

.each関数呼び出しを内側のループから取り出すだけです。

    $(currentLI).each(function () {
        if ($(this).hasClass('c_on')) { 
            var functionName = pieFunction[$(this).parent().attr('title') + 'Pie'].fun;
            pieIndex.push($(this).attr('data-index')); 
        }
    })
    generatePieURL(functionName, curQI, crrentMonth, currentYear, pieIndex);
于 2012-04-19T13:42:42.337 に答える
0

jQueryは同期的であるため、のgeneratePieURL後にを使用できます。eacheach

それで:

var functionName; // will be filled in the following loop (only if the output will be the same at each loop
$(currentLI).each(function(){
  if($(this).hasClass('c_on'))
    ...
    return false; // to stop the each, if not needed anymore
})
if pieIndex.length > 0
  generatePieUrl(...)
于 2012-04-19T13:41:46.183 に答える