首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自动完成搜索中的建议框未显示

自动完成搜索中的建议框未显示
EN

Stack Overflow用户
提问于 2020-10-13 15:34:03
回答 1查看 167关注 0票数 1

我想使用Photon服务创建一个自动完成的输入框。我尝试做的是创建一个返回结果的ajax请求。通过输入地址的几个字母,Photon API应该会向我显示结果,如图所示:

我所写的内容如下:

html

代码语言:javascript
复制
  <div class="frmSearch">
    <input type="text" id="search-box" placeholder="Country Name" />
    <div id="suggesstion-box"></div>
</div>

js

代码语言:javascript
复制
$("#search-box").keyup(function(){
        $.ajax({
        type: "GET",
        url: "http://photon.komoot.de/api/?q=" + $("#search-box").val(),

        beforeSend: function(){
            $("#search-box").css("background","#FFF url(LoaderIcon.gif) no-repeat 165px");
        },
        success: function(results){

       var aList = results.features;
       var aOptions = [];
       for (i=0; i < aList.length; i++) {
           optKey = aList[i].geometry.coordinates[0]+','+aList[i].geometry.coordinates[1];
           optLabel = aList[i].properties.name+', '+aList[i].properties.street+' '+aList[i].properties.housenumber+', '+
              aList[i].properties.city+', '+aList[i].properties.postcode;
           aOptions.push({
              "value": optKey,
              "label": optLabel
           });
       }

   

            $("#suggesstion-box").show();
            $("#suggesstion-box").html(aOptions);
            $("#search-box").css("background","#FFF");
        }
        });
    });

API调用运行得很好,但是我的建议框没有出现。我哪里做错了?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-13 16:04:56

不能直接使用html()函数添加对象。您只能传递string

因此,您需要将对象数组解析为string

代码语言:javascript
复制
$("#search-box").keyup(function() {
  $.ajax({
    type: "GET",
    url: "https://photon.komoot.de/api/?q=" + $("#search-box").val(),

    beforeSend: function() {
      $("#search-box").css("background", "#FFF url(LoaderIcon.gif) no-repeat 165px");
    },
    success: function(results) {

      var aList = results.features;
      var aOptions = [];
      let htmlVal = '';
      for (i = 0; i < aList.length; i++) {
        optKey = aList[i].geometry.coordinates[0] + ',' + aList[i].geometry.coordinates[1];
        optLabel = aList[i].properties.name + ', ' + aList[i].properties.street + ' ' + aList[i].properties.housenumber + ', ' +
          aList[i].properties.city + ', ' + aList[i].properties.postcode;
        aOptions.push({
          "value": optKey,
          "label": optLabel
        });
        htmlVal += `${optKey} ${optLabel} <br />`; // add each value to htmlVal
      }

      $("#suggesstion-box").show();
      $("#suggesstion-box").html(htmlVal);
      $("#search-box").css("background", "#FFF");
    }
  });
});
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="frmSearch">
  <input type="text" id="search-box" placeholder="Country Name" />
  <div id="suggesstion-box"></div>
</div>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64330626

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档