下拉列表包含4个相同的选项

时间:2017-05-31 18:29:25

标签: javascript forms select dropdown

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

具有4个可能性的下拉选择菜单非常有用。

这里的问题。如果菜单与墨西哥相同(墨西哥城市和墨西哥国家),它将无法正常工作。

我如何纠正?谢谢你的帮助!

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);    
<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

的原始改编

1 个答案:

答案 0 :(得分:0)

我注意到此代码段中有一些错误。

我想说的不是将墨西哥的城市宣布为“墨西哥”,为什么不宣布它为“墨西哥城”呢?根据我的理解,这是该城市正式被认可的内容,您的代码也将认识到这一点。因此,我将“墨西哥”改为“墨西哥城”并且运作良好。这也可以让目标受众更轻松地理解您生成的代码。

我注意到的第二个错误是你在'清单4'中拼错了'瓜达拉哈拉'。

另外,你有list4 select的getValue。删除并更改为this.value。如果你需要获取值,只需使用JS调用id,如果这是你的意图。

replace()将抛出一个错误,但它仍能正常工作,因为如果对前一个下拉列表(list3)进行了更改,它将继续避免将多个值添加到最终下拉列表(list4)。 / em>的

以下是正确运行的代码。

请告诉我这是否有帮助!

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);    
<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>