gpt4 book ai didi

javascript - 如何将链接附加到各个数组项?

转载 作者:行者123 更新时间:2023-11-28 08:49:27 25 4
gpt4 key购买 nike

我已经工作了几个小时试图让它发挥作用。我想做的是:首先,有两个下拉菜单,第一个控制第二个。然后我希望能够将每个单独的“第二选择”链接到 href。但是,“第二选择”位于数组中,我不知道如何将它们访问到 html 中。请帮忙。

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Any Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<script type="text/javascript">

var firmwares = [];
firmwares[1] = ["Sacramento","San Diego","San Francisco","Los Angeles"];
firmwares[2] = ["Cleveland","Akron","Canton","Cincinnati","Columbus"];
firmwares[3] = ["Philadelphia","Pittsburgh","Harrisburgh"];
firmwares[4] = [];

function fillSelect(nValue,nList){

nList.options.length = 1;
var curr = firmwares[nValue];
for (each in curr)
{
var nOption = document.createElement('option');
nOption.appendChild(document.createTextNode(curr[each]));
nOption.setAttribute("value",curr[each]);
nList.appendChild(nOption);
}
}
</script>

</head>
<body>
<form method="post" action="">
<div>
<select name='phones' onchange="fillSelect(this.value,this.form['firmwares'])">
<option value="">Select Your State</option>
<option value="1">California</option>
<option value="2">Ohio</option>
<option value="3">Pennsylvania</option>
<option value="4">Alaska</option>
</select>
<select name='firmwares' >
<option value="">Select Your City</option>
</select>
</div>
</form>
</body>
</html>

最佳答案

试试这个

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Any Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<script type="text/javascript">

var firmwares = {};
firmwares[1] = {"Sacramento" : "http://www.abc.com",
"San Diego" : "http://www.abc.com",
"San Francisco" : "http://www.abc.com",
"Los Angeles" : "http://www.abc.com"};
firmwares[2] = {"Cleveland" : "http://www.abc.com",
"Akron" : "http://www.abc.com",
"Canton" : "http://www.abc.com",
"Cincinnati" : "http://www.abc.com",
"Columbus" : "http://www.abc.com"};
firmwares[3] = {"Philadelphia" : "http://www.abc.com",
"Pittsburgh" : "http://www.abc.com",
"Harrisburgh" : "http://www.abc.com"};
firmwares[4] = {};

function fillSelect(nValue,nList){

nList.options.length = 1;
var curr = firmwares[nValue];
for (each in curr)
{
var nOption = document.createElement('option');
nOption.appendChild(document.createTextNode(each));
nOption.setAttribute("value",each);
nList.appendChild(nOption);
}

document.getElementById("gotoCity").style.display = "none";
}

function openCity(firmware, city) {
var gotoCity = document.getElementById("gotoCity");
gotoCity.href = firmwares[firmware][city];
gotoCity.style.display="";
}
</script>

</head>
<body>
<form method="post" action="">
<div>
<select name='phones' onchange="fillSelect(this.value,this.form['firmwares'])">
<option value="">Select Your State</option>
<option value="1">California</option>
<option value="2">Ohio</option>
<option value="3">Pennsylvania</option>
<option value="4">Alaska</option>
</select>
<select name='firmwares' onchange="openCity(this.form['phones'].value, this.value)" >
<option value="">Select Your City</option>
</select>
<a id="gotoCity" href="#" style="display:none;">Go</href>
</div>
</form>
</body>
</html>

关于javascript - 如何将链接附加到各个数组项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19341692/

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