This question already has an answer here:
这个问题在这里已有答案:
- How can I create a two dimensional array in JavaScript? 40 answers
- 如何在JavaScript中创建二维数组? 40个答案
- Is it possible to create an empty multidimensional array in javascript/jquery? 6 answers
- 是否可以在javascript / jquery中创建一个空的多维数组? 6个答案
I need to create global two dimensional array in jquery or javascript
我需要在jquery或javascript中创建全局二维数组
My function is like this
我的功能是这样的
<script>
var globalArray[0] = new Array();
function createArray(){
alert(globalArray[0]);
}
</script>
<div><input type='button' value='save' onclick='createArray();'> </div>
On click of that button I am getting this error "globalArray[0] is undefined"
点击该按钮我收到此错误“globalArray [0]未定义”
How can I create global dynamic multi dimensional array.
如何创建全局动态多维数组。
2 个解决方案
#1
7
if (!globalArray[index])
globalArray[index] = []; // init the array.
globalArray[index].push(name);
You have a typo with the dot:
你有一个点的拼写错误:
$.("#uname").val();
Change to:
改成:
$("#uname").val();
What are you trying to do with this code?
你想用这个代码做什么?
Update: (The question was totally edited.)
更新:(问题已完全编辑。)
Your code:
你的代码:
var globalArray[0] = new Array();
globalArray[0]
is invalid variable name, you need first to declare the array:
globalArray [0]是无效的变量名,需要先声明数组:
var globalArray = []; // Array literal.
globalArray[0] = [] // The element at position 0 is new an array.
#2
1
Intead of
Intead of
if(loop == 0){
globalArray[index][0] = uname;
}else{
globalArray[index][loop++] = uname;
}
Use this
用这个
if(loop > 0){
globalArray[index][loop++] = uname;
}else{
globalArray[index][0] = uname;
}
#1
7
if (!globalArray[index])
globalArray[index] = []; // init the array.
globalArray[index].push(name);
You have a typo with the dot:
你有一个点的拼写错误:
$.("#uname").val();
Change to:
改成:
$("#uname").val();
What are you trying to do with this code?
你想用这个代码做什么?
Update: (The question was totally edited.)
更新:(问题已完全编辑。)
Your code:
你的代码:
var globalArray[0] = new Array();
globalArray[0]
is invalid variable name, you need first to declare the array:
globalArray [0]是无效的变量名,需要先声明数组:
var globalArray = []; // Array literal.
globalArray[0] = [] // The element at position 0 is new an array.
#2
1
Intead of
Intead of
if(loop == 0){
globalArray[index][0] = uname;
}else{
globalArray[index][loop++] = uname;
}
Use this
用这个
if(loop > 0){
globalArray[index][loop++] = uname;
}else{
globalArray[index][0] = uname;
}