代码之家  ›  专栏  ›  技术社区  ›  saltyfries

使用javascript/jquery按字母顺序对输入的单词数组排序

  •  -3
  • saltyfries  · 技术社区  · 7 年前

    我希望页面有一个弹出框,用户在其中键入一个位置(输入存储在数组中),然后继续键入,直到他们键入“done”,然后页面按字母顺序用逗号加载位置列表。我得到了第一部分ok(提示和输入文本),但我的页面在输入“完成”后没有加载输入的材料。以下是我的代码:

            <div id="outputPlaces"></div>
    
            <script>
                $(document).ready(function() {
                    var favPlaces = [];
                    var input = prompt("Please enter your favorite place or type done to stop entering places.");
                    while (input != 'done') {
                        favPlaces.push(input);
                        input = prompt("Please enter another favorite place or type done to stop entering places.");
                    }
                    favPlaces.sort();
                    $('#outputPlaces').html = favPlaces.join(", ")
    
                });
    
            </script>
    
    2 回复  |  直到 7 年前
        1
  •  -1
  •   Matthew    7 年前

    看起来你有 favWords favPlaces

    .html(favPlaces.join(", "))

        2
  •  -1
  •   Kelly Adrian Neri    7 年前

    因为favWords没有定义。也许你指的是favPlaces。加入(“,”)并尝试更改$(“#outputPlaces”)。html=favPlaces。将(“,”)连接到文档。getElementById(“outputPlaces”)。innerHTML=favPlaces。连接(“,”)