gpt4 book ai didi

javascript - 选择标签未显示任何选项

转载 作者:行者123 更新时间:2023-12-03 02:26:32 25 4
gpt4 key购买 nike

                     <!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>Parallel Array Lookup II</title>
<script>
window.onload=initialize();

function initialize()
{
// do this only if the browser can handle DOM methods
if (document.getElementById)
{

aRegionalOffices = ["New York", "Chicago", "Houston", "Portland"];
aRegionalManagers = ["Shirley Smith", "Todd Gaston", "Leslie Jones",
"Harold Zoot"];
aRegOfficeQuotas = [300000, 250000, 350000, 225000];
// point to the critical input fields & save in global variables
oSelect = document.getElementById('offices');
oManager = document.getElementById('manager');
oQuota = document.getElementById('quota');
// if they all exist...
if (oSelect && oManager && oQuota)
{
// build the drop-down list of regional offices
for (var i = 0; i < aRegionalOffices.length; i++)
{
oSelect.options[i] = new Option(aRegionalOffices[i]);
}
// set the onchange behavior
//addEvent(oSelect, ‘change’, getData);
oSelect.addEventListener('change',getData);
}
// plug in data for the default select option
getData();
}
}

function getData(evt)
{
// get the offset of the selected option
var index = oSelect.selectedIndex;
// get data from the same offset in the parallel arrays
oManager.value = aRegionalManagers[index];
oQuota.value = aRegOfficeQuotas[index];
}

</script>

</head>
<body>
<h1>Parallel Array Lookup</h1>
<form id="officeData" action="" method="post">
<p>
<label for="offices">Select a regional office:</label>
<select id="offices" name="offices">
</select>
</p> <p>
<label for="manager">The manager is:</label>
<input type="text" id="manager" name="manager" size="35" />
</p>
<p>
<label for="quota">The office quota is:</label>
<input type="text" id="quota" name="quota" size="8" />
</p> </form>
</body>
</html>

在这里,当用户选择任何区域办事处时,我尝试显示区域经理姓名及其办事处配额。

但是在选择区域办事处时,没有出现任何选项,提示无法读取 SelectedIndex null 的属性。这里我创建了三个数组来存储区域办事处、regionalMangers 和办事处配额值。

然后,我使用 for 循环将区域办事处值添加为 select 标记内的选项。

Error Screenshot

最佳答案

window.onload= function_name(避免编写 function_name();)

希望这会起作用

关于javascript - 选择标签未显示任何选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48920910/

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