Javascript for循环获取最后一个值 [英] Javascript for loop getting last value

查看:165
本文介绍了Javascript for循环获取最后一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在asp.net我使用javascript获取输入值,如果我只考虑单个值,那部分就完成了...现在我正在计算数据库中的值,因为我有100条记录用于此目的我需要循环这里。我得到的值只是最后一条记录在javascript中for循环的问题。



我尝试了什么:



var count = total; < ---这个总数用代码计算

for(var i = 1; i< = count; i ++)

alert(count);

在计算中,我获得了第100个记录。但我需要从第一个到百个记录如何实现这个

解决方案

请看这里:用于循环和警报的Javascript - Stack Overflow [ ^ ] - Paulpro的答案。


乍一看我以为你遇到了由<$ c创建的问题JavaScript中的$ c> var ,并建议使用 let 。 :笑:



但在看完问题之后,我才知道也许你需要这样做,

 for(var i = 1; i< = count; i ++){
alert(i);
}

这也解决了你所面临的问题,

Quote:

在计数中我得到第100条记录

这是因为count设置为100(在你的代码中)。



另外,你甚至没有从任何地方访问任何元素 - 集合[i] - 那么你怎么能说你获得第100名呢?你的意思是说,你总是100?



请看以下链接,

scope - 使用let和var在JavaScript中声明变量有什么区别? - 堆栈溢出 [ ^ ]

let | MDN [ ^ ]

for | MDN [ ^ ]



您也可以尝试在我创建的这个在线小提琴中探索循环,编辑小提琴 - JSFiddle [ ^ ]


In asp.net i am using javascript for getting the input values and that part completed if i considered only single value... Now i am counting values from database in that i am having 100 records for this purpose i require loop here. I am getting values only the last record what is issue with for loop in javascript.

What I have tried:

var count = total; <--- this total is calculated in code behind
for(var i=1;i<=count;i++)
alert(count);
In count i am getting 100th record. But i require from first to hundred records how can i achieve this

解决方案

See here: Javascript for loop and alert - Stack Overflow[^] - Paulpro's answer.


At the first glance I thought you were experiencing issues created by the var in JavaScript and was going to suggest the usage of let. :laugh:

But after reading the question complete, I came to know that perhaps you need to do this,

for(var i = 1; i <= count; i++) {
   alert(i);
}

And that also answers the problem you are facing,

Quote:

In count i am getting 100th record

That is because count is set to be 100 (in your code behind).

Also, you are not even accessing any element from anywhere—collection[i]—so how can you say that you are getting 100th? Did you mean to say, you get 100 always?

Please see the following links,
scope - What's the difference between using "let" and "var" to declare a variable in JavaScript? - Stack Overflow[^]
let | MDN[^]
for | MDN[^]

You can also try to explore the loop in this online fiddle that I created, Edit fiddle - JSFiddle[^]


这篇关于Javascript for循环获取最后一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆