我正在尝试创建一个二维数组-但是在JS控制台中,它一直在说我的“ Randarray”函数未定义。我似乎无法弄清楚为什么它是不确定的,也许我只需要另一双眼睛就可以看到它。
任何帮助表示赞赏!
第2部分
<button id="array" onclick="Randarray()"> Array </button>
<script>
function Randarray()
{
randarray = new Array(5)
randarray [0] = new Array(3)
randarray [0][0] = "Toyota"
randarray [0][1] = "1998"
randarray [0][2] = "Black"
randarray [1] = new Array(3)
randarray [1][0] = "Ferrari"
randarray [1][1] = "2006"
randarray [1][2] = "Red"
randarray [2] = new Array(3)
randarray [2][0] = "Ferrari"
randarray [2][1] = "2006"
randarray [2][2] = "Red"
randarray [3] = new Array(3)
randarray [3][0] = "Jeep"
randarray [3][1] = "2006"
randarray [3][2] = "Silver"
randarray [4] = new Array(3)
randarray [4][0] = "Mercedes"
randarray [4][1] = "2016"
randarray [4][2] = "White"
randarray [5] = new Array(3)
randarray [5][0] = "BMW"
randarray [5][1] = "2017"
randarray [5][2] = "Black"
function getArray(row,col){
document.arrayForm.myResult.value=randarray[row][col];
}
document.getElementById('array').addEventListener('click', Randarray);
</script>
最佳答案
您忘记了Randarray
功能之前先关闭getArray
功能。做这个:
} // Missing.
function getArray(row,col)
另外,不要使用事件监听器和
onclick
内联函数。<button id="array" onclick="Randarray()">Array</button>
<script>
function Randarray() {
randarray = new Array(5)
randarray[0] = new Array(3)
randarray[0][0] = "Toyota"
randarray[0][1] = "1998"
randarray[0][2] = "Black"
randarray[1] = new Array(3)
randarray[1][0] = "Ferrari"
randarray[1][1] = "2006"
randarray[1][2] = "Red"
randarray[2] = new Array(3)
randarray[2][0] = "Ferrari"
randarray[2][1] = "2006"
randarray[2][2] = "Red"
randarray[3] = new Array(3)
randarray[3][0] = "Jeep"
randarray[3][1] = "2006"
randarray[3][2] = "Silver"
randarray[4] = new Array(3)
randarray[4][0] = "Mercedes"
randarray[4][1] = "2016"
randarray[4][2] = "White"
randarray[5] = new Array(3)
randarray[5][0] = "BMW"
randarray[5][1] = "2017"
randarray[5][2] = "Black"
}
function getArray(row, col) {
document.arrayForm.myResult.value = randarray[row][col];
}
document.getElementById('array').addEventListener('click', Randarray);
</script>
关于javascript - Randarray未定义,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39987010/