ASP数组JavaScript数组 [英] ASP array to JavaScript array

查看:143
本文介绍了ASP数组JavaScript数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的问题:

我有一个一维数组,它是在相同的.asp一些服务器端code的结果。我想利用这个阵列,其上的串转移到一个JavaScript数组,这样我就可以在表中使用的值显示的目的。

I have a one-dimensional array that is a result of some server side code in the same .asp. I want to take this array and transfer the strings in it to a javascript array, so I can use the values in a table for displaying purposes.

这是code我曾尝试:

This is the code I have tried:

<script language="javascript">
var jsArray = [];
var n = <%=r%>;

for (n = 0; n < 4; n++){
    jsArray[n] = '<%=tulemused(n)%>'; //this isn't working for some reason
}

</script>

我个人认为是由 N 变量产生的错误。当我把它写在段落它显示正常,但在上面标注的线路上使用时,它不工作。我也认为for循环可能是问题。

I personally think the error is created by the n variable. It is displayed correctly when I write it in a paragraph, but it doesn't work when used in the line marked above. I have also considered that the for loop could be the problem.

我在做什么错了?

推荐答案

您&LT;%= - 当页面呈现像code将被执行。这意味着,它是书面的方式,即code将再次当页面呈现,试图访问数组的第n个元素被执行。然而,变量n不会在当时访问的范围。

Your "<% ="-like code will be executed when the page is rendered. That means, the way it is written, that code will be executed once when the page is rendered, trying to access the n-th element of the array. However, the variable n would NOT be in the scope of access at that time.

因此​​,如果你希望你使用这样的code,你需要还包括迭代在嵌入式code。

Hence, if you wish you use such code, you need to include the iteration also in the embedded code.

这是更简单的解决方法是存储字符串列表(你想传递)作为一个单独的连接字符串,由已知的分隔符分隔,并访问连接字符串中的Javascript和它JS分裂。

An easier workaround would be to store the list of strings (that you wish to pass) as a single concatenated string, separated by a known delimiter and access the concatenated string in Javascript and split it in JS.

也就是说,如果字符串变量 tulemused_concatenated 持有串联字符串,由分隔符分隔',',下面的脚本应该工作。

That is, if the string variable tulemused_concatenated holds the concatenated strings, separated by the delimiter ',', the following script should work.

<script language="javascript">
var jsArray = [];
var n = <%=r%>;

var concatenated_string = '<%=tulemused_concatenated%>'; //this isn't working for some reason
var jsArray[n] = concatenated_string.split(',');

</script>

这篇关于ASP数组JavaScript数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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