首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >4级相同选择的下拉列表

4级相同选择的下拉列表
EN

Stack Overflow用户
提问于 2017-05-31 18:29:24
回答 1查看 44关注 0票数 1

我在网上找到了这个修改过的脚本。

它的工作非常好的下拉选择菜单与4个位置。

这里的问题。如果菜单和墨西哥一样(墨西哥是城市,墨西哥是国家),它就不能正常工作。

我怎么才能纠正呢?谢谢你帮忙!

代码语言:javascript
复制
var categories = [];

// list1

categories["startList"] = ["America","Europe"]

// list2

categories["America"] = ["USA","Mexico"];
categories["Europe"] = ["France","UK"];

// list3

categories["USA"] = ["New York","Texas"];;
categories["Mexico"] = ["Mexico","Guadalajara"];
categories["France"] = ["Alsace","Normandie"];
categories["UK"] = ["Wales", "Scotland", "England"];

// list4

categories["New York"] = ["Manhattan","Brooklyn","Harlem","Queens"];
categories["Texas"] = ["Dallas","Eagle Pass"];

categories["Mexico"] = ["DF"];
categories["Guadalaraja"] = ["East","West"];

categories["Alsace"] = ["Strasbourg","Kronenbourg"];
categories["Normandie"] = ["Caen","Saint-Malo","Saint-Pierre","Saint-Jean"];

categories["Wales"] = ["Cardiff", "New Port"];
categories["Scotland"] = ["Edimbourg"];
categories["England"] = ["London","Manchester","Exeter","Dover"];



var nLists = 4; // number of select lists in the set

function fillSelect(currCat,currList){
var step = Number(currList.name.replace(/\D/g,""));
for (i=step; i<nLists+1; i++) {
document.forms['tripleplay']['List'+i].length = 1;
document.forms['tripleplay']['List'+i].selectedIndex = 0;
}
var nCat = categories[currCat];
for (each in nCat) {
var nOption = document.createElement('option'); 
var nData = document.createTextNode(nCat[each]); 
nOption.setAttribute('value',nCat[each]); 
nOption.appendChild(nData); 
currList.appendChild(nOption); 
} 
} 

function init() {
fillSelect('startList',document.forms['tripleplay']['List1'])
}

navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', init, false) : addEventListener('load', init, false);    
代码语言:javascript
复制
<form name="tripleplay" action="">
<select name='List1' onchange="fillSelect(this.value,this.form['List2'])">
<option selected>Select One</option>
</select>
&nbsp;
<select name='List2' onchange="fillSelect(this.value,this.form['List3'])">
<option selected>Select Two</option>
</select>
&nbsp;
<select name='List3' onchange="fillSelect(this.value, this.form['List4'])">
<option selected >Select Three</option>
</select>
&nbsp;
<select name='List4' onchange="getValue(this.value, this.form['List3'].value, this.form['List2'].value, 
this.form['List1'].value)">
<option selected >Select Four</option>
</select>
</form>

https://jsfiddle.net/nbz9atmv/https://www.sitepoint.com/community/t/country-state-city-dropdown-list/2438的原始改编

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-31 18:57:50

我注意到了这个片段中的一些错误。

我想说的是,为什么不宣布墨西哥的城市为“墨西哥”,为什么不宣布为“墨西哥城”?据我所知,这就是城市被正式认可的地方,你的代码也会认识到这一点。所以,我把“墨西哥”改为“墨西哥城”,效果非常好。这还允许目标受众更容易地理解生成的代码。

我注意到的第二个错误是'Guadalajara‘在'list 4’中拼写错误。

此外,您还为getValue选择了list4。移除并更改为this.value。如果您需要获取值,只需使用JS调用id,如果这是您的意图。

替换()将抛出一个eror,但它仍然正常工作,因为如果对前一个下拉列表(List4)进行了更改,它将继续避免将多个值添加到最终下拉列表(List3)。

下面是正确运行的代码。

如果这有帮助的话请告诉我!

代码语言:javascript
复制
var categories = [];

// list1

categories["startList"] = ["America","Europe"]

// list2

categories["America"] = ["USA","Mexico"];
categories["Europe"] = ["France","UK"];

// list3

categories["USA"] = ["New York","Texas"];;
categories["Mexico"] = ["Mexico City","Guadalajara"];
categories["France"] = ["Alsace","Normandy"];
categories["UK"] = ["Wales", "Scotland", "England"];

// list4

categories["New York"] = ["Manhattan","Brooklyn","Harlem","Queens"];
categories["Texas"] = ["Dallas","Eagle Pass"];

categories["Mexico City"] = ["DF"];
categories["Guadalajara"] = ["East","West"];

categories["Alsace"] = ["Strasbourg","Kronenbourg"];
categories["Normandy"] = ["Caen","Saint-Malo","Saint-Pierre","Saint-Jean"];

categories["Wales"] = ["Cardiff", "New Port"];
categories["Scotland"] = ["Edimbourg"];
categories["England"] = ["London","Manchester","Exeter","Dover"];



var nLists = 4; // number of select lists in the set

function fillSelect(currCat,currList){
var step = Number(currList.name.replace(/\D/g,""));

for (i=step; i<nLists+1; i++) {
document.forms['tripleplay']['List'+i].length = 1;
document.forms['tripleplay']['List'+i].selectedIndex = 0;
}
var nCat = categories[currCat];
for (each in nCat) {
var nOption = document.createElement('option'); 
var nData = document.createTextNode(nCat[each]); 
nOption.setAttribute('value',nCat[each]); 
nOption.appendChild(nData); 
currList.appendChild(nOption); 
} 
} 

function init() {
fillSelect('startList',document.forms['tripleplay']['List1'])
}

navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', init, false) : addEventListener('load', init, false);    
代码语言:javascript
复制
<form name="tripleplay" action="">
<select name='List1' onchange="fillSelect(this.value,this.form['List2'])">
<option selected>Select One</option>
</select>
&nbsp;
<select name='List2' onchange="fillSelect(this.value,this.form['List3'])">
<option selected>Select Two</option>
</select>
&nbsp;
<select name='List3' onchange="fillSelect(this.value, this.form['List4'])">
<option selected >Select Three</option>
</select>
&nbsp;
<select name='List4' onchange="fillSelect(this.value, this.form['List3'].value, this.form['List2'].value, 
this.form['List1'].value)">
<option selected >Select Four</option>
</select>
</form>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44292239

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档