通过 JQuery 从 JS 数组填充菜单而不重复
我有一个问题,大约两天无法克服。
我有一个分隔字符串,我想通过 JQuery 填充菜单。
我的数组是这样的;
Country1,City1|Country1,City2|Country2,City1|Country3,City1|Country3,City2|Country3,City3
我想使用这个菜单结构通过使用 JQuery append
函数(或任何其他建议的方式)
我的意思是,当用户将鼠标悬停在我的菜单上时,会触发一个菜单,并且将显示所有国家/地区,并且每个国家/地区都有自己的子菜单元素(城市)。
我想指出的是,存在多少个国家,每个国家有多少个城市都是未知的。
我使用unique
函数来消除重复的国家,但我无法将城市放入国家。
马上谢谢..
i have a problem and I could not overcome about two days.
I have an delimited string and i want to populate a menu via JQuery.
My array like this;
Country1,City1|Country1,City2|Country2,City1|Country3,City1|Country3,City2|Country3,City3
and i want to use this menu structure by using JQuery append
function (or any other suggested way)
I mean, when the user hover over my menu trigger a menu will pop up and all Countries will be shown and each one has own sub menu elements (Cities).
i would like to point that, how many Countries exist and each Contry has how many cities are unknown.
I used unique
function to eleminate duplicated Countries but i cound not put Cities into Countries.
Thanks right now..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅此工作示例: http://jsfiddle.net/nrabinowitz/FDWgF/
您首先需要收集以国家/地区名称作为键的对象中每个国家/地区的城市,然后迭代该对象以创建列表:
See this working example: http://jsfiddle.net/nrabinowitz/FDWgF/
You first need to collect the cities for each country in an object with the country names as keys, then iterate through that object to create your list:
您的数组应该是这样的多维数组:
那么您将不会有重复的国家/地区,那么您应该使用
for in
循环遍历数组并以这种方式填充菜单your array should be a multi-dimensional array like this:
Then you will not have duplicate countries, then you should loop through the array with
for in
and populate the menu that way