有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

javascript jquery自动完成不过滤

我正在尝试使用JQuery autocomplete函数,其中包含一个ajax调用,它将返回字符串列表。。当我尝试输入时,它会显示所有字符串列表,而不是根据输入进行筛选。。我不确定我会错在哪里。。下面是我的自动完成功能

$("#domainNameId").autocomplete({
        source : function(request, response) {
            console.log("in ajax ");
            $.ajax({
                url : "getAllDomains",
                type : "GET",
                contentType : "application/json",
                data : {
                    env : $("#environment").val()
                },
                dataType : "json",
                success : function(data) {
                     response(data); // list of strings..
                },
                error : function(x, t, m) {
                    console.trace();
                    if (!(console == 'undefined')) {
                        console.log("ERROR: " + x + t + m);
                    }
                    console.log(" At the end");
                }
            });

        },
    });

谢谢你的帮助


共 (2) 个答案

  1. # 2 楼答案

    您的后端似乎总是返回整个数据,而不进行任何筛选(服务名称本身是^{)。在这种情况下,不需要使用源选项的函数形式来进行ajax调用

    您所做的是在用户输入时向服务器发送多个AJAX请求。如果后端总是返回相同的数据,那么向其发送多个请求就没有意义了。您只需获取一次数据,然后以响应作为源初始化autocomplete,然后小部件将按照用户类型进行过滤

    文件说:

    A response callback, which expects a single argument: the data to suggest to the user. This data should be filtered based on the provided term.

    因此,如果您的服务器不进行过滤,就不要使用函数表单发出AJAX请求

    做一些类似的事情:

    $(function() {
      // make a one-time request
      $.ajax({
        url: "getAllDomains",
        type: "GET",
        contentType: "application/json",
        dataType: "json",
        success: function(data) {
          // init the widget with response data and let it do the filtering
          $("#domainNameId").autocomplete({
            source: data
          });
        },
        error: function(x, t, m) {
          console.trace();
          if (!(console == 'undefined')) {
            console.log("ERROR: " + x + t + m);
          }
          console.log(" At the end");
        }
      });
    
    });