通过 JQuery 从 JS 数组填充菜单而不重复

发布于 2024-12-10 02:39:51 字数 504 浏览 0 评论 0原文

我有一个问题,大约两天无法克服。
我有一个分隔字符串,我想通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒儿 2024-12-17 02:39:52

请参阅此工作示例: http://jsfiddle.net/nrabinowitz/FDWgF/

您首先需要收集以国家/地区名称作为键的对象中每个国家/地区的城市,然后迭代该对象以创建列表:

var data = "Country1,City1|Country1,City2|Country2,City1|Country3,City1|Country3,City2|Country3,City3";

// split into country/city pairs
data = data.split('|');

var countries = {},
    pair, country, city;

for (var x=0; x<data.length; x++) {
    // get pair as an array
    pair = data[x].split(',');
    country = pair[0];
    city = pair[1];
    // make sure the country is in our object
    if (!(country in countries)) {
        countries[country] = []
    }
    // add the city to the list for that country
    countries[country].push(city);
}

// now get the values out of the object
$.each(countries, function(country, cities) {
    var $country = $('<li>' + country + '<ul></ul></li>');
    // append each city to sub-list
    $.each(cities, function(i, city) {
        $('ul', $country)
            .append('<li>' + city + '</li>')
    });
    // now append the whole thing to the country list
    $('#countries').append($country);
});

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:

var data = "Country1,City1|Country1,City2|Country2,City1|Country3,City1|Country3,City2|Country3,City3";

// split into country/city pairs
data = data.split('|');

var countries = {},
    pair, country, city;

for (var x=0; x<data.length; x++) {
    // get pair as an array
    pair = data[x].split(',');
    country = pair[0];
    city = pair[1];
    // make sure the country is in our object
    if (!(country in countries)) {
        countries[country] = []
    }
    // add the city to the list for that country
    countries[country].push(city);
}

// now get the values out of the object
$.each(countries, function(country, cities) {
    var $country = $('<li>' + country + '<ul></ul></li>');
    // append each city to sub-list
    $.each(cities, function(i, city) {
        $('ul', $country)
            .append('<li>' + city + '</li>')
    });
    // now append the whole thing to the country list
    $('#countries').append($country);
});
我不咬妳我踢妳 2024-12-17 02:39:52

您的数组应该是这样的多维数组:

var countries = {
"country1":["city1", "city2"],
"country2":["city1"],
"counrty3":["city2", "city2"]
};

那么您将不会有重复的国家/地区,那么您应该使用 for in 循环遍历数组并以这种方式填充菜单

var options = "";
foreach(countries as country)
{
    foreach(country as city)
    {
        options += "<option value=" + city+ ">"+ city+ "</options>";
    }
}
$("#cities").html(options)
<select id="cities">
</select>

your array should be a multi-dimensional array like this:

var countries = {
"country1":["city1", "city2"],
"country2":["city1"],
"counrty3":["city2", "city2"]
};

Then you will not have duplicate countries, then you should loop through the array with for in and populate the menu that way

var options = "";
foreach(countries as country)
{
    foreach(country as city)
    {
        options += "<option value=" + city+ ">"+ city+ "</options>";
    }
}
$("#cities").html(options)
<select id="cities">
</select>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文