我已经尝试将超链接添加到我的Javascript下拉列表,但是当我尝试这样做时,下拉列表不起作用。有三个下拉列表:使用Javascript添加超级链接到层叠下拉列表
- 州:加利福尼亚州,俄勒冈州)
- 县:加州有蒙特雷,Alemeda随着城市&俄勒冈州有一个城市即道格拉斯
- 城市:蒙特利的县,阿拉米达具有城市:
例如,如果用户选择:
- 州:加利福尼亚州
- 县:蒙特利
- 市:萨利纳斯
我想,这样在选择时,它萨利纳斯有一个超链接,它要么:
- 自动重定向到的超级链接或
- 有一个按钮来完成操作。
我如何去用下面的代码添加超链接到城市(最终下拉列表):
的Html
<form name="myform" id="myForm">
<select name="optone" id="stateSel" size="1">
<option value="" selected="selected">Select state</option>
</select>
<br>
<br>
<select name="opttwo" id="countySel" size="1">
<option value="" selected="selected">Please select state first</option>
</select>
<br>
<br>
<select name="optthree" id="citySel" size="1">
<option value="" selected="selected">Please select county first</option>
</select>
的Javascript
var stateObject = {
"California": {
"Monterey": ["Salinas", "Gonzales"],
"Alameda": ["Berkeley"]
},
"Oregon": {
"Douglas": ["Roseburg", "Winston"],
}
}
window.onload = function() {
var stateSel = document.getElementById("stateSel"),
countySel = document.getElementById("countySel"),
citySel = document.getElementById("citySel");
for (var state in stateObject) {
stateSel.options[stateSel.options.length] = new Option(state, state);
}
stateSel.onchange = function() {
countySel.length = 1; // remove all options bar first
citySel.length = 1; // remove all options bar first
if (this.selectedIndex < 1) {
countySel.options[0].text = "Please select state first"
citySel.options[0].text = "Please select county first"
return; // done
}
countySel.options[0].text = "Please select county"
for (var county in stateObject[this.value]) {
countySel.options[countySel.options.length] = new Option(county, county);
}
if (countySel.options.length==2) {
countySel.selectedIndex=1;
countySel.onchange();
}
}
stateSel.onchange(); // reset in case page is reloaded
countySel.onchange = function() {
citySel.length = 1; // remove all options bar first
if (this.selectedIndex < 1) {
citySel.options[0].text = "Please select county first"
return; // done
}
citySel.options[0].text = "Please select city"
var cities = stateObject[stateSel.value][this.value];
for (var i = 0; i < cities.length; i++) {
citySel.options[citySel.options.length] = new Option(cities[i], cities[i]);
}
if (citySel.options.length==2) {
citySel.selectedIndex=1;
citySel.onchange();
}
}
}
前往espacarello的答案在这里。这将有助于https://stackoverflow.com/questions/12287672/links-in-select-dropdown-options –
@Daniel Zuzevich这不是一个单独的下拉列表。这个问题与我的不同,我已经看到了。我的问题中的下拉项目被放置在JavaScript文件本身中,如我的代码所示。 – Sebastian