简体   繁体   中英

Javascript can't use "i" variable inside of for loop

I have a line chart template from CanvasJS. I was trying to use for loop inside that. Loop condition works fine. But when I use i variable inside the for, it doesn't work and visual studio doesn't recognize i . But instead of using i , if I use a number - like 5 or 10 (doesn't matter)- it works fine.

Where is the problem?

for(var i=0;i< <%=this.something%>;i++)
{
chart.data[0].addTo("dataPoints", {x: <%=this.historyDate[i]%>, y: <%=this.historyPoint[i]%>})       
});

PS: historyDate and historyPoint are defined in code behind. So i had to use them like that. I'm kind of new to programming. So im sorry if i made a mistake

You are missing server side code with client side code <%= exp %> is ASP.NET syntax and it's processed on the server side. i is declared in Javascript which will run client side in the browser so the server can't use it

You need to serialize this.historyDate as JSON and put in in a javascript variable, if it isn't too big. Or find some other design.

The i variable belongs to javascript, but you are using it inside your template language <%=this.historyDate[i]%> . So there i is not defined

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