简体   繁体   中英

One after another call for same event in javascript

I have an event over a drop down menu. Now whenever this event happens I have to read table data, and alter it. I am reading table data using $("table").each(function(){}); function and again altering it using the same code. But these both each functions are working parallel while I want them to execute one after another. My code seems like:

$(document).on('change','#rank-q1',function(){
    $('#rank-q2').val('rank-asc');
    var data=$(this).val().split('-');
    var i=-1;
    var j=0;
    var k=0;
    //console.log(results);
    $("#tbody-emps tr").each(function(){
        k++;
        if($(this).find('td').length>6){
            var temp=alasql('select '+data[1]+' from '+data[0]+' where '+tableChoice[data[0]]+' = ?',[results[i][j][1]])[0];
            if(data[1]=='birthday' || data[1]=='grad'){
                results[i][j][0]=Date.parse(temp[data[1]]);
            }else{
                results[i][j][0]=temp[data[1]];
            }
            results[i][j][2]=$(this).html();
            console.log(results[i][j][2]);  ///Note here
        }else{
            if(i>=0){
                results[i].sort(function(a,b){
                    return a[0]>b[0]?-1:(a[0]<b[0]?1:0);
                });             
            }
            i++;
            j=0;
        }
    });
    results[i].sort(function(a,b){
        return a[0]>b[0]?-1:(a[0]<b[0]?1:0);
    });
    
    console.log(results);

    var i=-1,j=0;
    $("#tbody-emps tr").each(function(){
        if($(this).find('td').length>6){
            $(this).html(results[i][j][2]);
            j++;
        }else{
            i++;
            j=0;
        }
    });
    console.log(results);
});

Since $('table').each(function(){}); is being used two times it is executed at the same time. What should i do? FYI: All console.log(results) are giving me same old value of results array while, when i am using console.log(results[i][j][2]) it is showing me updated information. That's why it seems like those each() functions are running at the same time, without updating.

do this

$('table').each(function(){
    //Some data capture and alter data
}).each(function(){
    //Modification in table data.
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM