简体   繁体   English

jQuery:如何处理自闭合标签和多个循环?

[英]jQuery: How do I deal with self-closing tags and multiple loops?

I have 3 arrays: one with a list of cities, one with a list of states (which correspond to the cities), and one that's just a list of states with the duplicates removed. 我有3个数组:一个带有城市列表,一个带有州列表(与城市相对应),另一个只是删除了重复项的州列表。

I'm trying to generate a list that looks like this: 我正在尝试生成一个看起来像这样的列表:

  • State 1 状态1
    • City 1 城市1
    • City 2 城市2
  • State 2 状态2
    • City 3 城市3

Here's what I've got: 这是我得到的:

$.each(stateArrayUnq, function(i) {
    $('#list').append("<li>" + stateArrayUnq[i] + "<ul>");
    $.each(stateArray, function(j) {
        if (stateArray[j] == stateArrayUnq[i]) {
            $('#list').append("<li>" + cityArray[j] + "<\/li>");
        }
    });
    $('#list').append("<\/ul><\/li>");
});

I know I can't structure my code like this without having the browser auto-close my tags prematurely, but unfortunately I don't have the slightest idea how to rebuild this. 我知道, 如果没有浏览器过早地自动关闭标签, 就无法构建这样的代码,但是不幸的是,我丝毫不知道如何重建它。 I've read through a few related threads, but I'm still pretty confused. 我已经阅读了一些相关的主题,但是我仍然很困惑。 I think I'm supposed to set the "append" code to variables or something, but I don't know how to handle the loops. 我认为应该将“附加”代码设置为变量或其他内容,但是我不知道如何处理循环。

I appreciate any help you can give. 感谢您提供的任何帮助。 Thanks a bunch! 谢谢一群!

UPDATE: Here are my arrays: 更新:这是我的数组:

var cityArray = ["Concord", "Lafayette", "Lewisville", "Madison", "NW Freeway Houston", "North Miami", "Casselberry", "South Fort Myers", "SW Freeway", "Woodbury", "Tarpon Springs"]
var stateArray = ["North Carolina", "Louisiana", "Texas", "Wisconsin", "Texas", "Florida", "Florida", "Florida", "Texas", "Minnesota", "Florida"]
var statearrayUnq = ["Florida", "Louisiana", "Minnesota", "North Carolina", "Texas", "Wisconsin"]

You can do this by building an html string which is stored in a variable. 您可以通过构建存储在变量中的html字符串来做到这一点。 When you are done with your logic you can append the build html string. 完成逻辑后,您可以附加构建html字符串。

 //Test data var cityArray = ["Concord", "Lafayette", "Lewisville", "Madison", "NW Freeway Houston", "North Miami", "Casselberry", "South Fort Myers", "SW Freeway", "Woodbury", "Tarpon Springs"]; var stateArray = ["North Carolina", "Louisiana", "Texas", "Wisconsin", "Texas", "Florida", "Florida", "Florida", "Texas", "Minnesota", "Florida"]; var stateArrayUnq = ["Florida", "Louisiana", "Minnesota", "North Carolina", "Texas", "Wisconsin"]; //Generate the html string and append it. var html = ""; $.each(stateArrayUnq, function(i) { html += "<li>" + stateArrayUnq[i] + "<ul>"; $.each(stateArray, function(j) { if (stateArray[j] == stateArrayUnq[i]) { html += ("<li>" + cityArray[j] + "<\\/li>"); } }); html += "<\\/ul><\\/li>"; }); $('#list').append(html); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="list"> <p>test</p> </div> 

If list were a ul , you could nest them using actual jQuery objects like so. 如果listul ,则可以像这样使用实际的jQuery对象嵌套它们。

 var cityArray = ["Concord", "Lafayette", "Lewisville", "Madison", "NW Freeway Houston", "North Miami", "Casselberry", "South Fort Myers", "SW Freeway", "Woodbury", "Tarpon Springs"]; var stateArray = ["North Carolina", "Louisiana", "Texas", "Wisconsin", "Texas", "Florida", "Florida", "Florida", "Texas", "Minnesota", "Florida"]; var data = cityArray.map((c,i) => ({state: stateArray[i], city: c})); //flatten the data var $list = data.reduce(($ul, item) => { var $cityLi = $(`<li>${item.city}</li>`); //Create <li> for city var $stateLi = $ul.children(`li[data-state='${item.state}']`); //Find state <li> if (!$stateLi.length) //Create state <li> if it doesn't yet exist $stateLi = $(`<li data-state='${item.state}'>${item.state}<ul></ul></li>`); $stateLi.children("ul").append($cityLi).end().appendTo($ul); //Add the city to the state return $ul; }, $("<ul />")); $("body").append($list); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

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

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