【问题标题】:The autocomplete-window is not popping up自动完成窗口没有弹出
【发布时间】:2022-01-19 20:05:52
【问题描述】:

我有这个输入,我想用下面的脚本自动完成。 url 返回一个字符串列表。 当我输入时,数据会显示在控制台中,但不会弹出自动完成窗口。

可能出了什么问题?

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
    <input type="text" class="form-control my-input" name="from" id="from">

    <script>
        $(document).ready(function () {
            $("#from").keyup(function (string) {
                $.ajax({
                    type: "GET",
                    url: "http://127.0.0.1:5000/complete?station=" + $(this).val(),
                    success: function (data) {
                        $("#from").autocomplete({
                            source: data
                        });
                        console.log(data)
                    }
                });
            });
        });
    </script>
</body>
</html>

【问题讨论】:

    标签: javascript html jquery ajax


    【解决方案1】:

    您应该将jQuery-ui.min.cssjquery.min.jsjquery-ui.min.js 文件添加到项目中。我使用模拟数据开发的以下应用程序成功运行。我在错误字段中调用了getList()方法来模拟来自服务器的响应。

    应用测试图片如下:

    应用源代码如下:

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" />
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
      <title>Document</title>
    </head>
    
    <body>
      Input: <input type="text" id="inputElement" onkeyup="myFunction(this.value)">
    
      <script>
        function myFunction(value) {
          /* alert(`URL: http://127.0.0.1:5000/complete?station=${value}`); */
          $.ajax({
            type: "GET",
            url: "http://127.0.0.1:5000/complete?station=" + value,
            success: function (data) {
              if (data != '') {
                $("#inputElement").autocomplete({
                  source: data
                });
                console.log(data)
              }
            },
            /* I simulated the successful response from the server in the field below. */
            error: function (data) {
              $("#inputElement").autocomplete({
                source: getList()
              });
            }
          });
        }
    
        function getList() {
          var availableTags = ["ActionScript", "AppleScript", "Asp", "BASIC", "C", "C++", "Clojure",
            "COBOL", "ColdFusion", "Erlang", "Fortran", "Groovy", "Haskell", "Scala",
            "Java", "JavaScript", "Lisp", "Perl", "PHP", "Python", "Ruby", "Scheme"
          ];
          return availableTags;
        }
      </script>
    </body>
    </html>

    【讨论】:

      【解决方案2】:

      做到了:

                $("#from").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        url: 'http://127.0.0.1:5000/complete?station=' + $("#from").val(),
                        dataType: "json",
                        success: function (data) {
                            response(data);
                        },
                        error: function (xhr, status, error) {
                            alert("Error");
                        }
                    });
                }
            });
      

      【讨论】:

      • 什么是“这个”?你改变了什么,为什么?请在您的答案中添加一些解释,以便其他人可以从中学习
      • 请解释你的答案。
      • 是的,对不起。这是我在 youtube 上找到的另一种方法。
      猜你喜欢
      • 2011-12-03
      • 2013-07-14
      • 1970-01-01
      • 1970-01-01
      • 2017-08-01
      • 1970-01-01
      • 2014-07-27
      • 2020-03-24
      • 2016-01-07
      相关资源
      最近更新 更多