简体   繁体   English

Javascript 不能在 for 循环内使用“i”变量

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

I have a line chart template from CanvasJS.我有一个来自 CanvasJS 的折线图模板。 I was trying to use for loop inside that.我试图在里面使用 for 循环。 Loop condition works fine.循环条件工作正常。 But when I use i variable inside the for, it doesn't work and visual studio doesn't recognize i .但是当我在 for 中使用i变量时,它不起作用并且 Visual Studio 无法识别i But instead of using i , if I use a number - like 5 or 10 (doesn't matter)- it works fine.但是,如果我不使用i ,而是使用一个数字——比如 5 或 10(无关紧要)——它工作正常。

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. PS:historyDate 和historyPoint 是在后面的代码中定义的。 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.您缺少客户端代码的服务器端代码<%= exp %>是 ASP.NET 语法,它是在服务器端处理的。 i is declared in Javascript which will run client side in the browser so the server can't use it i在 Javascript 中声明,它将在浏览器中运行客户端,因此服务器无法使用它

You need to serialize this.historyDate as JSON and put in in a javascript variable, if it isn't too big.如果它不是太大,您需要将this.historyDate序列this.historyDate JSON 并放入一个 javascript 变量中。 Or find some other design.或者找一些其他的设计。

The i variable belongs to javascript, but you are using it inside your template language <%=this.historyDate[i]%> . i 变量属于 javascript,但您在模板语言<%=this.historyDate[i]%>中使用它。 So there i is not defined所以我没有定义

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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