搜索窗口小部件 - 如何确定是否按下了“输入”或选择了结果?

时间:2016-10-13 05:50:29

标签: javascript esri arcgis-js-api

我有一个搜索框,在搜索地址时会抛出某些自动建议的值。

现在有很多方法可以触发搜索事件:

  1. 用户开始输入地址(忽略自动建议的值)并点击 Enter key
  2. 用户开始在整个过程中键入地址,从自动建议列表中选择
  3. 在这种情况下,有没有办法可以判断是否在事件处理程序中选择了 Enter 键?

    的Javascript

    var esriMap = new Map("esriMap", {
        basemap: "topo",
        center: [-12.4260, 31.3403],
        zoom: 12
    });
    var search = new Search({
       map: esriMap,
    }, dom.byId("esriSearch"));
    search.startup();
    search.on("select-result", searchboxResult); // the event handler
    
    function searchboxResult(e)
    {
      // determine "Enter" key versus "Selection from auto-suggest"
    }
    

1 个答案:

答案 0 :(得分:1)

当然,有一种方法可以确定在地图上显示选择时是否进行了选择或按下了Enter键。

以下是实现此目的的工作代码 -



require([

        "esri/map",
        "esri/dijit/Search",
        "dojo/on",
        "dojo/domReady!"

      ], function (Map, Search, on) {
         var map = new Map("map", {
            basemap: "gray",
            center: [-120.435, 46.159], // lon, lat
            zoom: 7
         });

         var search = new Search({
            map: map
         }, "search");
         search.startup();
         
        var isEnter= false;
        on(search.inputNode, "keypress", function(evt){
          isEnter = evt.keyCode == 13;
        });
        
        search.on("select-result", searchboxResult); 

         function searchboxResult(e)
           {
            alert(isEnter?"By Enter Selection": "By Suggestion Selection");
            // determine "Enter" key versus "Selection from auto-suggest"
          }

      });

html,
      body,
      #map {
         height: 100%;
         width: 100%;
         margin: 0;
         padding: 0;
      }
      #search {
         display: block;
         position: absolute;
         z-index: 2;
         top: 20px;
         left: 74px;
      }

<link rel="stylesheet" href="https://js.arcgis.com/3.18/esri/themes/calcite/dijit/calcite.css">
   <link rel="stylesheet" href="https://js.arcgis.com/3.18/esri/themes/calcite/esri/esri.css">

<script src="https://js.arcgis.com/3.18/"></script>

<body class="calcite">
   <div id="search"></div>
   <div id="map"></div>
</body>
&#13;
&#13;
&#13;

希望这会对您有所帮助:)