gpt4 book ai didi

javascript - 未捕获的类型错误 : Cannot read property '0' of undefined in select box

转载 作者:行者123 更新时间:2023-11-28 00:05:52 25 4
gpt4 key购买 nike

我不明白为什么会收到错误,但最终结果实际上是我想要的。这是我的功能:

function populate(){
for(var i = 0; i < locations.length; i += 1){
var select = document.getElementById("end");
select.options[select.options.length] = new Option(buildings[i][0], buildings[i][3]);
}
};

它从这个数组中提取:

buildings = [
['Allied Health', '', 'buildings', '35.206815, -101.908493', 2, "http://www.actx.edu/staff/images/icons/ac.png"],
['Building A', '', 'buildings', '35.206718, -101.906127', 3, "http://www.actx.edu/staff/images/icons/ac.png"],
['Building B', '', 'buildings', '35.207614, -101.907383', 4, "http://www.actx.edu/staff/images/icons/ac.png"],
['Building C', '', 'buildings', '35.207545, -101.905702', 5, "http://www.actx.edu/staff/images/icons/ac.png"],
['Building D', '', 'buildings', '35.207408, -101.906485', 6, "http://www.actx.edu/staff/images/icons/ac.png"],
['Child Development Center', '', 'buildings', '35.207409, -101.908901', 7, "http://www.actx.edu/staff/images/icons/ac.png"],
['Gymnastics/Dance', '', 'buildings', '35.207264, -101.909682', 8, "http://www.actx.edu/staff/images/icons/ac.png"],
['Jones Hall', '', 'buildings', '35.207514, -101.908236', 9, "http://www.actx.edu/staff/images/icons/ac.png"],
['Lecture Hall', '', 'buildings', '35.206754, -101.907246', 10, "http://www.actx.edu/staff/images/icons/ac.png"],
['N.W. Library', '', 'buildings', '35.206543, -101.904580', 11, "http://www.actx.edu/staff/images/icons/ac.png"],
['Delta Building', '', 'buildings', '35.208272, -101.905817', 12, "http://www.actx.edu/staff/images/icons/ac.png"]
];

并填充此加载:

<body onload="populate();">
<select class="form-control" id="end" onchange="calcRoute();">
<option disabled selected> -- Select a Destination -- </option>
</select>

它实际上做了我想要它做的事情,即将建筑物名称及其坐标放入选择框中。但我在 chrome 控制台中收到此错误:

Uncaught TypeError: Cannot read property '0' of undefined

它指向这一行:

select.options[select.options.length] = new Option(buildings[i][0], buildings[i][3]);

我不确定为什么它会在 Chrome 控制台中抛出错误,但仍然可以按预期工作。

最佳答案

也许你的i变量刚刚超过 buildings长度。例如,如果您有 20 个地点和 15 座建筑物,则当浏览器计算 new Option(buildings[15][0], buildings[15][3]) 时它抛出这个错误。

编辑:更具体地说,假设我们有 15 栋建筑物,我们的数组的索引将从 0 到 14,因此建筑物[15] 是 undefined 。这就是错误的来源,您正在尝试阅读 buildings[15][0]这就像阅读 undefined[0] .

您应该发布locations变量也为了获得更好的响应。

var buildings = [

['Allied Health', '', 'buildings', '35.206815, -101.908493', 2, "http://www.actx.edu/staff/images/icons/ac.png"],
['Building A', '', 'buildings', '35.206718, -101.906127', 3, "http://www.actx.edu/staff/images/icons/ac.png"],
['Building B', '', 'buildings', '35.207614, -101.907383', 4, "http://www.actx.edu/staff/images/icons/ac.png"],
['Building C', '', 'buildings', '35.207545, -101.905702', 5, "http://www.actx.edu/staff/images/icons/ac.png"],
['Building D', '', 'buildings', '35.207408, -101.906485', 6, "http://www.actx.edu/staff/images/icons/ac.png"],
['Child Development Center', '', 'buildings', '35.207409, -101.908901', 7, "http://www.actx.edu/staff/images/icons/ac.png"],
['Gymnastics/Dance', '', 'buildings', '35.207264, -101.909682', 8, "http://www.actx.edu/staff/images/icons/ac.png"],
['Jones Hall', '', 'buildings', '35.207514, -101.908236', 9, "http://www.actx.edu/staff/images/icons/ac.png"],
['Lecture Hall', '', 'buildings', '35.206754, -101.907246', 10, "http://www.actx.edu/staff/images/icons/ac.png"],
['N.W. Library', '', 'buildings', '35.206543, -101.904580', 11, "http://www.actx.edu/staff/images/icons/ac.png"],
['Delta Building', '', 'buildings', '35.208272, -101.905817', 12, "http://www.actx.edu/staff/images/icons/ac.png"]
];

var locations = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,];

function populate() {
for(var i = 0; i < locations.length; i += 1){
var select = document.getElementById("end");
select.options[select.options.length] = new Option(buildings[i][0], buildings[i][3]);
}
};
<body onload="populate();">
<select class="form-control" id="end" onchange="calcRoute();">
<option disabled selected> -- Select a Destination -- </option>
</select>

此代码片段显示了我正在讨论的内容(如果您打开浏览器控制台,您应该会看到相同的错误,这只是由 locations 数组长于 buildings 引起的)。

var buildings = [

['Allied Health', '', 'buildings', '35.206815, -101.908493', 2, "http://www.actx.edu/staff/images/icons/ac.png"],
['Building A', '', 'buildings', '35.206718, -101.906127', 3, "http://www.actx.edu/staff/images/icons/ac.png"],
['Building B', '', 'buildings', '35.207614, -101.907383', 4, "http://www.actx.edu/staff/images/icons/ac.png"],
['Building C', '', 'buildings', '35.207545, -101.905702', 5, "http://www.actx.edu/staff/images/icons/ac.png"],
['Building D', '', 'buildings', '35.207408, -101.906485', 6, "http://www.actx.edu/staff/images/icons/ac.png"],
['Child Development Center', '', 'buildings', '35.207409, -101.908901', 7, "http://www.actx.edu/staff/images/icons/ac.png"],
['Gymnastics/Dance', '', 'buildings', '35.207264, -101.909682', 8, "http://www.actx.edu/staff/images/icons/ac.png"],
['Jones Hall', '', 'buildings', '35.207514, -101.908236', 9, "http://www.actx.edu/staff/images/icons/ac.png"],
['Lecture Hall', '', 'buildings', '35.206754, -101.907246', 10, "http://www.actx.edu/staff/images/icons/ac.png"],
['N.W. Library', '', 'buildings', '35.206543, -101.904580', 11, "http://www.actx.edu/staff/images/icons/ac.png"],
['Delta Building', '', 'buildings', '35.208272, -101.905817', 12, "http://www.actx.edu/staff/images/icons/ac.png"]
];

var locations = [0, 0, 0, 0, 0, 0, 0, 0, 0,];

function populate() {
for(var i = 0; i < locations.length; i += 1){
var select = document.getElementById("end");
select.options[select.options.length] = new Option(buildings[i][0], buildings[i][3]);
}
};
<body onload="populate();">
<select class="form-control" id="end" onchange="calcRoute();">
<option disabled selected> -- Select a Destination -- </option>
</select>

正如您所看到的,第二个片段可以正常工作而不会出现错误(只是因为我 trim 了 locations 数组的一部分)。我建议你改变一下

for(var i = 0; i < locations.length; i += 1){

for(var i = 0; i < buildings.length; i += 1){

为了始终显示您在这个(最后一个)片段中看到的所有建筑物:

var buildings = [

['Allied Health', '', 'buildings', '35.206815, -101.908493', 2, "http://www.actx.edu/staff/images/icons/ac.png"],
['Building A', '', 'buildings', '35.206718, -101.906127', 3, "http://www.actx.edu/staff/images/icons/ac.png"],
['Building B', '', 'buildings', '35.207614, -101.907383', 4, "http://www.actx.edu/staff/images/icons/ac.png"],
['Building C', '', 'buildings', '35.207545, -101.905702', 5, "http://www.actx.edu/staff/images/icons/ac.png"],
['Building D', '', 'buildings', '35.207408, -101.906485', 6, "http://www.actx.edu/staff/images/icons/ac.png"],
['Child Development Center', '', 'buildings', '35.207409, -101.908901', 7, "http://www.actx.edu/staff/images/icons/ac.png"],
['Gymnastics/Dance', '', 'buildings', '35.207264, -101.909682', 8, "http://www.actx.edu/staff/images/icons/ac.png"],
['Jones Hall', '', 'buildings', '35.207514, -101.908236', 9, "http://www.actx.edu/staff/images/icons/ac.png"],
['Lecture Hall', '', 'buildings', '35.206754, -101.907246', 10, "http://www.actx.edu/staff/images/icons/ac.png"],
['N.W. Library', '', 'buildings', '35.206543, -101.904580', 11, "http://www.actx.edu/staff/images/icons/ac.png"],
['Delta Building', '', 'buildings', '35.208272, -101.905817', 12, "http://www.actx.edu/staff/images/icons/ac.png"]
];

function populate() {
for(var i = 0; i < buildings.length; i += 1){
var select = document.getElementById("end");
select.options[select.options.length] = new Option(buildings[i][0], buildings[i][3]);
}
};
<body onload="populate();">
<select class="form-control" id="end" onchange="calcRoute();">
<option disabled selected> -- Select a Destination -- </option>
</select>

关于javascript - 未捕获的类型错误 : Cannot read property '0' of undefined in select box,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31344631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com