搜索小部件 - 如何确定是否按下了 "Enter" 或选择了结果?

Search Widget - How to determine if "Enter" was pressed or result was selected?

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

现在有多种方式可以触发搜索事件:

  1. 用户开始输入地址(忽略自动建议的值)并按下 Enter
  2. 用户开始输入地址,并在过程中途从自动建议的列表中选择一个值

在这种情况下,有没有一种方法可以确定是进行了选择还是在事件处理程序中按下了 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"
}

当然,有一种方法可以判断在地图上显示选择时是进行了选择还是按下了回车键。

下面是实现这个的工作代码-

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>

希望对您有所帮助:)