简体   繁体   English

无法使用其他值填充其他下拉菜单-Javascript

[英]Cannot populate other drop down menus with other values- Javascript

I seem to have a problem with Javascript. 我似乎对Javascript有问题。 I am currently trying to use arrays to populate dropdown menus. 我目前正在尝试使用数组填充下拉菜单。 They are dynamically appearing drop down menus. 它们是动态出现的下拉菜单。 In my example, the countries and cities and activites are populating correctly for USA. 在我的示例中,美国的国家和城市以及活动场所正确填充。 However, for Canada and UK, the activities list is still using the USA's activities list. 但是,对于加拿大和英国,活动列表仍在使用美国的活动列表。 Can someone tell me what I'm doing wrong? 有人可以告诉我我在做什么错吗? If what I just explained seems a bit confusing, please take a look at the jsfiddle I made here: http://jsfiddle.net/KCz3G/ 如果我刚才解释的内容有些混乱,请看一下我在这里制作的jsfiddle: http : //jsfiddle.net/KCz3G/

<title>Page Title</title>

If you use this as a start to store the data, things might get easier: 如果以此为开始存储数据,事情可能会变得更容易:

Simple demo: http://jsfiddle.net/KCz3G/3/ 简单的演示: http : //jsfiddle.net/KCz3G/3/

var cities = {
 usa: ['New York|newyorkvalue', ...],
 uk: ['London|londonvalue', ...]
};

var activities = {
 newyorkvalue: ['xxx|yyy', ...],
 londonvalue: ['xxx|yyy', ...]
};

//get cities in usa
for (i=0;i<cities["usa"].length;i++) {

}

Which mean you use/pass the "value text", like londonvalue as a parameter instead of an index to find which array of "something" to populate. 这意味着您使用/传递“值文本”,例如londonvalue作为参数而不是index以查找要填充的“内容”数组。

It will give you much more flexibility, still using arrays of values. 仍然使用值数组,它将为您提供更大的灵活性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM