简体   繁体   中英

Passing variable to jquery eq()

I have been trying to figure out why I can't pass a variable called timepos to a td:eq() in jquery and nothing I try seems to work.

Here is the relevant parts of code:

var timepos = 0;
function count() {

     var secs;

     secs = Number(time_chunks[2]);
     secs++;

     if ((secs == 00) || (secs % 5 == 0)) {
         for (i = 0; i < 6; i++) {
             $('.gridclassscrolled tbody tr:eq(' + i + ') td:eq(' + timepos + ')').find('.txttime').css("background-color", "yellow");
         };
         timepos = timepos + 1;
     };
}

It does not work. If I replace td:eq(' + timepos + ') with an integer it works, but does not increment like I need it to. I have also tried using parseInt. When I alert the variable timepos it clearly prints an incrementing integer. I'm confused why it isn't working.

You are trying to incremente timepos outside the for loop. This means timepos will always be zero.

for (i = 0; i < 6; i++) {
    $('.gridclassscrolled tbody tr:eq(' + i + ') td:eq(' + timepos + ')').find('.txttime').css("background-color", "yellow");
    timepos++;
};

So I figured out the issue and it was completely unrelated to the code I had posted. Sorry for the confusion. I suppose it would have helped to post the code in its entirety but those variables were tied into other functions and it was affecting several gridviews, so the whole code would have been about 200 lines.

Just for the record the timepos variable does need to be outside the for loop. The count() function is triggered by a timer that is affected by user actions. Since the count function runs each second, I needed timepos to be outside that entire function. Otherwise if timepos were declared in the count() function it would reset each second.

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