【问题标题】:Dropdown with 4 Levels identical selections具有 4 个级别相同选择的下拉菜单
【发布时间】:2017-11-01 16:47:07
【问题描述】:

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

拥有 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的原创改编

【问题讨论】:

  • 我不确定它是否能解决问题,但categories 绝对应该是@​​987654323@ 而不是数组。 var categories = {}

标签: javascript forms select dropdown


【解决方案1】:

我注意到这个 sn-p 中有一些错误。

我想说,与其将墨西哥的城市宣布为“墨西哥”,不如将其宣布为“墨西哥城”?据我了解,这是该城市被正式认可的内容,您的代码也将认可这一点。所以,我把“墨西哥”这个城市改成了“墨西哥城”,效果很好。这也使您的目标受众更容易理解生成的代码。

我注意到的第二个错误是“列表 4”中的“瓜达拉哈拉”拼写错误。

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

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

下面是正确运行的代码。

如果这有帮助,请告诉我!

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>

【讨论】:

    猜你喜欢
    • 2021-01-16
    • 2013-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多